unfortunately I can only find very specific questions on stackoverflow and no exact details in the documentation, so here is a more general example that may help others too.
I just want to add a value to the array (arr) in the object with the "title: 'title3".
{
_id: <id>,
prop1: val1,
prop2: val2,
prop3: [
{
title: 'title1',
arr: ['val1', 'val2', 'val3'],
},
{
title: 'title2',
arr: ['val1', 'val2', 'val3'],
},
{
title: 'title3',
arr: ['val1', 'val2', 'val3'], //only update this array
}
]
}
My current approach looks something like this:
SomeModel.findOneAndUpdate(
{ _id: id, "prop3.title": "title3" },
{$push: { "prop3.$[].arr": someDoc._id.toString() }},
(err, doc) => {
if (err) {
console.log('Error updating doc: ', err);
resolve(false);
} else {
resolve(doc);
}
}
);
However, the problem here is that a value is added not only in the array of the object with the title "title3", but everywhere.
How can I add a value exclusively to the array in the object with the title value "title3"? I would also be very grateful for links to documentation explaining this.