mongodb - Increment all array elements -
i have document this:
{ "_id" : objectid("534527e489e46c16787bda0f"), "packages" : [ { number: 1 }, { number: 2 } ] }
i want increment packages.number
@ once. possible?
i tried db.collection.update({}, {$inc: {"packages.number": 1}})
didn't work.
no isn't possible , reason given in use of positional $
operator. , says when used ever first match found.
so did not try match in array, in value in positional operator use index in:
db.collection.update( { "packages.number": 1 }, { "$inc": { "packages.$.number": 1 } } )
but in general cannot update members of array @ once, can retrieve document , update in client code before writing back.
Comments
Post a Comment