简体   繁体   English

如果mongodb中不存在孙子元素,如何添加它?

[英]How to add grand children elements if it do not exist in mongodb?

I need to add new element to grand children element.我需要向大子元素添加新元素。 For example I need to add new warehouse to existing product.例如,我需要向现有产品添加新仓库。 In this example all the sizes.wares have LAX, however "NYC" is missing from product "aaaa", size "42" and product "bbbb", size "45"在此示例中,所有尺寸.wares 都有 LAX,但是产品“aaaa”、尺寸“42”和产品“bbbb”、尺寸“45”中缺少“NYC”

So I need to do check all products and theirs sizes if ware "NYC" exist if not then add new ware "NYC"所以我需要检查所有产品及其尺寸,如果商品“NYC”存在,则添加新商品“NYC”

[
{
"_id": ObjectId("5db72c636309f84479ec0c7b"),
"productCode": "aaaa",
"brand": "Nike",
"image": "some.jpg",
"sizes": [
    {
    "_id": ObjectId("5db72c636309f84479ec0c7e"),
    "size": "41",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c80"),
        "ware": "LAX",
        "amount": 100
        },
        {
        "_id": ObjectId("5db72c636309f84479ec0c7f"),
        "ware": "NYC",
        "amount": 7
        }
    ]
    },
    {
    "_id": ObjectId("5db72c636309f84479ec0c7c"),
    "size": "42",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c7d"),
        "ware": "LAX",
        "amount": 16
        }
    ]
    }
]
},
{
"_id": ObjectId("5db72c636309f84479ec0c7X"),
"productCode": "bbbb",
"brand": "Nike",
"image": "some.jpg",
"sizes": [
    {
    "_id": ObjectId("5db72c636309f84479ec0c7D"),
    "size": "41",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c8G"),
        "ware": "LAX",
        "amount": 100
        },
        {
        "_id": ObjectId("5db72c636309f84479ec0c7R"),
        "ware": "NYC",
        "amount": 7
        }
    ]
    },
    {
    "_id": ObjectId("5db72c636309f84479ec0c7q"),
    "size": "45",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c7n"),
        "ware": "LAX",
        "amount": 16
        }
    ]
    }
]
}]

For example product "bbbb" size "45"例如产品“bbbb”尺寸“45”

{
    "_id": ObjectId("5db72c636309f84479ec0c7q"),
    "size": "45",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c7n"),
        "ware": "LAX",
        "amount": 16
        }
    ]
    }

should this look like this after the "upsert"在“upsert”之后这应该是这样的吗

{
    "_id": ObjectId("5db72c636309f84479ec0c7q"),
    "size": "45",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c7n"),
        "ware": "LAX",
        "amount": 16
        },{
        "_id": ObjectId("5db72c636309f84479ec0c7x"),
        "ware": "NYC",
        "amount": 0
        }

    ]
    }

Would be this one:会是这个:

db.collection.updateOne(
   { productCode: "bbbb" },
   {
      $push: {
         "sizes.$[item].wares": {
            _id: ObjectId("5db72c636309f84479ec0c7a"),
            ware: "NYC",
            amount: 0
         }
      }
   },
   { arrayFilters: [{ "item.size": "45" }] }
)

Note, in order to avoid duplicates you may use $addToSet instead of $push .请注意,为了避免重复,您可以使用$addToSet而不是$push

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

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