Here is a simplified diagram of a document in my Model:
{
ar1: [
{
b: {
ar2: [{ _id: 1, value: 2 }],
},
},
{
b: {
ar2: [{ _id: 2, value: 2 }],
},
},
{
b: {
ar2: [{ _id: 1, value: 5 }],
},
},
];
}
Now I'm looking to update all elements of ar2 that have an _id equal to 1 so the result would be:
{
ar1: [
{
b: {
ar2: [{ _id: 1, value: 3 }],
},
},
{
b: {
ar2: [{ _id: 2, value: 2 }],
},
},
{
b: {
ar2: [{ _id: 1, value: 3 }],
},
},
];
}
The code snippet below does not achieve this:
Model.updateMany({
'ar1.b.ar2._id' : 1
},{
'ar1.$[].b.ar2.$[].value' : 2
});
Any recommendations on how to approach this?
The goal is to target specific elements within the nested array and update their values based on a query.