updateOne 查询仅修改数组的第一个匹配元素,尽管有多个匹配

问题描述 投票:0回答:1

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
的所有匹配元素。

node.js mongodb mongodb-query aggregation-framework
1个回答
0
投票

已修复:

            {
              $push: {
                "array_property.$[].img_array": {
                  $each: [new_img],
                },
              },
            }
© www.soinside.com 2019 - 2024. All rights reserved.