I have an object in MongoDB and trying to write a query that finds the document by company_id then goes through the purchases array and update paid to true in all record that matches with user_id, without overwriting the rest of the data.
{
_id: 27834682,
company_id: 27,
purchases: [
{
paid: false,
user_id: 19,
items: [{...}, {...}],
price: 1085
},
...
]
}
I was trying out
Model.updateMany({company_id: company_id}, {"purchases.$[]": {$set: {paid: true}}})
but it removed the other data in the object and could not even filtered by user_id.
What would be the correct approach for this query?