I want push new object to nested array if object with field ‘Cat’=1 not exists. Or if object with ‘Cat’=1 field exists then increment Up field in this object.
I cant write right syntax, help plz
Document:
{
"BlogId" : 1,
"Items" : [
{
"Cat" : 1,
"Up" : 555,
},
{
"Cat" : 2,
"Up" : 666,
}
]
}
Query (with wrong syntax):
db.exp.update({ BlogId: 1 },
[
{
$set: {
"Items.Cat": {
$cond: [
{ "Items.Cat": 1 } ,
{ $inc: { "Items.Up": 1 } },
{ $push: { "Items": { Cat: 1, Up: 555 }} }
]
}
}
}
])