This question already has an answer here:
- Multiple use of the positional `$` operator to update nested arrays 2 answers
I am using sub-documents in mongodb.
With one level of sub-documents, I can update documents with
Parent.findOneAndUpdate({ _id: parentId, 'children._id': childId }, {
$set: {
'children.$.name': name
}
}, (err, doc) => {
...
});
but I have problems doing the same for another level of sub-documents, i.e.
Parent.findOneAndUpdate({ _id: parentId, 'children._id': childId, 'children.grandchildren._id': grandchildId }, {
$set: {
'children.$.grandchildren.$.name': name
}
}, (err, doc) => {
...
});
Is the positional operator ($
) limited to only 1 level of subdocuments?