I have a document like this
_id:'111'
products:[
{
_id:'pqr'
nums:[
{_id:'aaa',
quantity:50
},
{_id:'bbb',
quantity:50
}
]
}
]
The document above can be summarized like this below for easy understanding.
_id
products: [
nums: [
{}, //quantity is in this object
{}
]
]
I need to increment the value of quantity in nums subdocument which is in products subdocument based on its _id.
This is what I have tried so far but it doesn't work as I don't know how to catch the _id inside nums object so as to update the specific object in that sub-subdocument array.
Shop.findOneAndUpdate(
{ "_id": '111', "products._id": 'pqr' },
{
"$inc": {
"products.$[].nums.quantity": 1
}
}
)
How can I achieve this?