Tuesday, 9 May 2017

MongoDB + Nodejs : Increment a specific value of an array in a nested array

my MongoDB documents have the following structure:

{ _id : 'some_ID',
  matrix:  [[0,0,0][0,0,0][0,0,0]]
}

I'd like my NodeJS server to increment the second value of the second array in matrix, given a specific ID.

i.e I 'd like to have:

{ _id : 'some_ID',
  matrix:  [[0,0,0][0,1,0][0,0,0]]
}

I wrongly tried:

db.collection('myCollection').update({ _id  :  'some_ID'  },
                                     { $inc :  { matrix[2][2] : 1 } }
                                    );

Any guess on the right way to do that ?

THANKS :)



via Yasol

No comments:

Post a Comment