简体   繁体   English

如何使用多嵌套数组文档 findOneAndUpdate 单个字段

[英]How to findOneAndUpdate single field with multi nested array documents

I'm stuck on how to update single value in multi nested array documents value with findOneAndUpdate .我被困在如何使用findOneAndUpdate更新多嵌套数组文档值中的单个值。

My condition goes like this:我的情况是这样的:

Update warehouse amount where the productCode is "abc123", size "41" in warehouse "Hamburg".更新仓库“汉堡”中productCode为“abc123”、大小“41”的仓库数量。

I just get back null or bot sizes 41 and 42.我刚回来 null 或机器人尺寸 41 和 42。

Here is the part of the doc:这是文档的一部分:

{
 "_id": ObjectId("xxxx636309f84479ec0c7b"),
 "productCode": "abc123",
 "brand": "Nike",
 "name": "aaa",
 "model": "Runner",
 "color": "Brown",
 "image": "shoe.jpg",
 "sizes": [{
   "_id": ObjectId("xxxxc636309f84479ec0c7e"),
   "size": "41",
   "wares": [{
     "_id": ObjectId("xxxx2c636309f84479ec0c80"),
     "ware": "Hamburg",
     "amount": 7
    },
    {
     "_id": ObjectId("5db72c636309f84479ec0c7f"),
     "ware": "Berlin",
     "amount": 7
    }
   ]
  },
  {
   "_id": ObjectId("5db72c636309f84479ec0c7c"),
   "size": "42",
   "wares": [{
    "_id": ObjectId("5db72c636309f84479ec0c7d"),
    "ware": "Hamburg",
    "amount": 16
   }]
  }
 ],
 "__v": 0
}

This is what I've tried:这是我尝试过的:

Product.findOneAndUpdate({
  "productCode": "abc123",
  "sizes.size": 41,
  "sizes.wares.ware": "Hamburg"
 }, {
  "$set": {
   "sizes.0.wares.amount": 99
  }
 }, {
  useFindAndModify: false
 },
 (err, products) => {
  if (err) {
   return res.status(422).send(err)
  }
  return res.json(products)
 }
);

How can I solve this?我该如何解决这个问题?

And to fulfill @ambianBeing, this is how it would be done with findOneAndUpdate :为了满足@ambianBeing,这就是使用findOneAndUpdate的方式:

Product.findOneAndUpdate({
    "productCode": "abc123",
    "sizes": {
        $elemMatch: {
            $and: [
                { size: "41" },
                {
                    wares: {
                        $elemMatch: {
                            ware: "Hamburg"
                        }
                    }
                }]
        }
    }
}, {
    $set: {
        "sizes.$[theSize].wares.$[theWare].amount": 99
    }
}, {
    arrayFilters: [{
        "theSize.size": "41"
    }, {
        "theWare.ware": "Hamburg"
    }]
})

Can be done using filtered positional operator $[<identifier>] which is nifty in use cases of nested array updation.可以使用过滤的位置运算符$[<identifier>]来完成,这在嵌套数组更新的用例中非常有用。

Query (Mongo Shell):查询(Mongo Shell):

db.collection.update(
  { productCode: "abc123" },
  { $set: { "sizes.$[outer].wares.$[inner].amount": 99 } },
  {
    arrayFilters: [{ "outer.size": "41" }, { "inner.ware": "Hamburg" }],
    multi: false
  }
);

Query with Mongoose Model:使用 Mongoose Model 查询:

Product.update(
  { productCode: "abc123" },
  { "sizes.$[outer].wares.$[inner].amount": 99 },
  {
    arrayFilters: [{ "outer.size": "41" }, { "inner.ware": "Hamburg" }],
    multi: false
  },
  (err, rawDoc) => {
    if (err) {
      console.error(err);
    }
    console.info(rawDoc);
  }
);

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

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