The update query:
db.exp.update( { BlogId: 1 },
[
{
$set: {
Items: {
$reduce: {
input: { $ifNull: [ "$Items", [] ] },
initialValue: { items: [], update: false },
in: {
$cond: [ { $eq: [ "$$this.Cat", INPUT_DOC.Cat ] },
{
items: {
$concatArrays: [
"$$value.items",
[ { Cat: "$$this.Cat", Up: { $add: [ "$$this.Up", 1 ] } } ],
]
},
update: true
},
{
items: {
$concatArrays: [ "$$value.items", [ "$$this" ] ]
},
update: "$$value.update"
}
]
}
}
}
}
},
{
$set: {
Items: {
$cond: [ { $eq: [ "$Items.update", false ] },
{ $concatArrays: [ "$Items.items", [ INPUT_DOC ] ] },
{ $concatArrays: [ "$Items.items", [] ] }
]
}
}
}
]
)
The update does the following:
- if the
Catexists increments theUpvalue by1 - if the
Catnot exists adds theINPUT_DOCto theItemsarray - If the
Itemsarray doesn’t exist, creates the array and adds theINPUT_DOC
Try with the object values: INPUT_DOC = { Cat: 1, Up: 555 }, or INPUT_DOC = { Cat: 3, Up: 888 }