Ashesh Khatri
Ashesh Khatri

Reputation: 162

Want to Update Array of Array Object Value in Mongo

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.

Upvotes: 2

Views: 1370

Answers (1)

Smart003
Smart003

Reputation: 1119

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.... enter image description here

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 enter image description here

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

see the changes.

Upvotes: 1

Related Questions