https://mongoplayground.net/p/zIhLOT8YXLl
document schema:
{
_id: ObjectId();
array_property: [ {_id: ObjectId(), img_array: ["string", "string"] }, { }, ... ]
}
query:
await user.updateOne(
{
_id: user_id,
array_property: { $elemMatch: { _id: { $in: arrary_of_id } } },
},
{
$push: {
"array_property.$.img_array": {
$each: [new_img],
},
},
}
);
despite multiple elements of array_property
matching the query, the $push
will only happen to the first matching element. The rest are not modified.
How to fix this? The query is intended to update all matching elements of array_property
.
2
Answers
use
arrayFilters
:https://mongoplayground.net/p/CwH6ZActsv_https://mongoplayground.net/p/CwH6ZActsv_
Try using
updateMany
updateOone is doing exactly as it sounds, updating one document.