Stack Exchange
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.

possible duplicate of How to change all the array elements in a mongodb document to a certain value?

I wan't mongo query to direct update to my All Inventory value. U have any idea about this???

The duplicate shows how you can do it. That is all..

In my case where condition at  "option.store.code" level then what should i have to do??

@AsheshKhatri have you tried by using find and modify?

@Smart003 wrong! findAndModify  has nothing to do here.

@AsheshKhatri is it so difficult to understand how the duplicate answer works?

@Smart003 I have use this query :
db.runCommand(
{
findAndModify: "new_product",
query: { "options.stores.$.code": "172" },
update: { $inc: { "options.stores.$.inventory": -1000 } }
}
)
But not update the record.

@AsheshKhatri see the below answer

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.

here I have multiple array in options in which I have similar "store.code" how will it work in this case
{
"options": [
{
"sku": "8903689984338",
"stores": [
{
"code": "AND1",
"inventory": -1000
}
],
},
{
"sku": "1742564789",
"stores": [
{
"code": "AND1",
"inventory": -1000
}
],
},
]
}

@AsheshKhatri as per the above query all the documents which matches the conditions will be update.

As you mentioned in the question "I want to update each inventory value where code value is "AND1". hence the above query will satisfy the statement

but i have multiple json object in "options" then i can't use "options.0.stores"  in this case i m struggling!! u have any other way to resolve this query ??

can you give the entire structure of your collection. if i know the collection structure i can help on this

How do i share with u ??

the above query will be update the value which was fetched first.....
since you are using the python script use any loop for this update...........
and made a slight modification------{$set:{"options.(loop variable).stores.$.inventory":}}

ya this can work but this i m looking for direct update using mongo query
