Want to Update Array of Array Object Value in Mongo

我的未来我决定 提交于 2020-01-01 18:04:34

问题


I have the following MongoDB document:

{
"_id": ObjectId(),
"sku": "V4696-DR-V33",
"options": [
    {
        "sku": "8903689984338",
        "stores": [
            {
                "code": "AND1",
                "zipcode": "110070",
                "inventory": -1000
            },
            {
                "code": "AND2",
                "zipcode": "201010",
                "inventory": -1000
            },
            {
                "code": "AND3",
                "zipcode": "411001",
                "inventory": -1000
            },
            {
                "code": "AND4",
                "zipcode": " 700020",
                "inventory": -1000
            },
            {
                "code": "AND5",
                "zipcode": "110015",
                "inventory": -1000
            }
        ],
        "price": 2199,
        "_id": ObjectId(),
        "size": "14"
    },
    {
        "sku": "1742564789",
        "stores": [
            {
                "code": "AND1",
                "zipcode": "110070",
                "inventory": -1000
            },
            {
                "code": "AND2",
                "zipcode": "201010",
                "inventory": -1000
            },
            {
                "code": "AND3",
                "zipcode": "411001",
                "inventory": -1000
            },
            {
                "code": "AND4",
                "zipcode": " 700020",
                "inventory": -1000
            },
            {
                "code": "AND5",
                "zipcode": "110015",
                "inventory": -1000
            }
        ],
        "price": 2199,
        "_id": ObjectId(),
        "size": "14"
    },

]
}

I want to update each inventory value where code value is "AND1" . I want query in mongo query or any python script to update whole document with nested value. I am very Stuck with This Issue.


回答1:


try the following query for update

db.stack.update({"options.0.stores":{$elemMatch:{code:"AND1"}}},{$set:{"options.0.stores.$.inventory":200}})

for check i'll update with images....

i had taken the your data as example

now i had used the above query db.stack.update({"options.0.stores":{$elemMatch:{code:"AND1"}}},{$set:{"options.0.stores.$.inventory":200}})

then the result is shown in the following image

i had update the inventory to 200 where code:"AND1"

see the changes.



来源:https://stackoverflow.com/questions/32733790/want-to-update-array-of-array-object-value-in-mongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!