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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -