简体   繁体   中英

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"

So I need to do check all products and theirs sizes if ware "NYC" exist if not then add new ware "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"

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

should this look like this after the "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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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