https://mongoplayground.net/p/zIhLOT8YXLl
文档架构:
{
_id: ObjectId();
array_property: [ {_id: ObjectId(), img_array: ["string", "string"] }, { }, ... ]
}
查询:
await user.updateOne(
{
_id: user_id,
array_property: { $elemMatch: { _id: { $in: arrary_of_id } } },
},
{
$push: {
"array_property.$.img_array": {
$each: [new_img],
},
},
}
);
尽管
array_property
的多个元素与查询匹配,但 $push
只会发生在第一个匹配元素上。其余的没有修改。
如何解决这个问题?该查询旨在更新
array_property
的所有匹配元素。
已修复:
{
$push: {
"array_property.$[].img_array": {
$each: [new_img],
},
},
}