MongoDB insert document "or" increment field if exists in array -


what try simple, have an array inside document ;

"tags": [      {         "t" : "architecture",         "n" : 12     },      {         "t" : "contemporary",         "n" : 2     },      {         "t" : "creative",         "n" : 1     },      {         "t" : "concrete",         "n" : 3     } ] 

i want push array of items array

["architecture","blabladontexist"] 

if item exists, want increment object's n value (in case architecture),

and if don't, add new item (with value of n=0) { "t": "blabladontexist", "n":0}

i have tried $addtoset, $set, $inc, $upsert: true many combinations , couldn't it.

how can in mongodb?

i don't believe possible in single command.

mongodb doesn't allow $set (or $setoninsert) , $inc affect same field in single command.

you'll have 1 update command attempt $inc field, , if doesn't change documents (n = 0), update $set field it's default value.


Comments