If I have a document containing an array of objects.
Data = {
_id: 1,
total_amount: 0,
subDataArray: [
{
_id: 1,
amount: 0
},
{
_id: 2,
amount: 1
},
...
]
}
Updating the total_amount can be done simply like this...
Data.updateOne({ _id }, { $inc: { total_amount: 1} })
However, incrementing the amounts in subDataArray is more complex. If I have an array of updates for Data.subDataArray
var newSubDataArray = [
{
_id: 1,
amount: 5
},
{
_id: 2,
amount: 5
},
]
Is there a way to increment all amounts in Data.subDataArray with just one operation? Keep in mind that newSubDataArray may not always contain every item present in subDataArray, so it would require upsert behavior.
Can this even be achieved?
EDIT: Just to clarify, each amount value in newSubDataArray could differ from one another. The goal is to increment each existing subDataArray item's amount by the respective value found in newSubDataArray based on matching IDs.