简体   繁体   English

Mongoose:查找和更新多个嵌套文档

[英]Mongoose : Find and Update Multiple Nested Documents

My Documents are as follows.我的文件如下。

{
    "order_id" : "1",
    "payment_status" : false,
    "items" : [
        {
            "item_id" : 1,
            "payment_status" : false,
        },
        {
            "item_id" : 2,
            "payment_status" : false,
        },
        {
            "item_id" : 3,
            "payment_status" : false,
        },
    ]
}

I need to update the fields payment_status for {"order_id":1} and {"item_id" : 1} and {"item_id" : 3} .我需要更新{"order_id":1}{"item_id" : 1}{"item_id" : 3}的字段payment_status Also, I need to update the same in bulk for matching conditions.此外,我需要批量更新相同的匹配条件。 Is this possible in mongoose?这在猫鼬中可能吗?

You want to be using $arrayFilters like so:你想像这样使用$arrayFilters

db.collection.updateMany({
  "order_id": "1"
},
{
  "$set": {
    "items.$[item].payment_status": true
  }
},
{
  arrayFilters: [
    {
      "item.item_id": {
        $in: [
          1,
          3
        ]
      }
    }
  ]
})

Mongo Playground蒙戈游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM