2

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.

9
  • possible duplicate of How to change all the array elements in a mongodb document to a certain value? Commented Sep 23, 2015 at 8:00
  • I wan't mongo query to direct update to my All Inventory value. U have any idea about this??? Commented Sep 23, 2015 at 8:39
  • The duplicate shows how you can do it. That is all.. Commented Sep 23, 2015 at 8:48
  • In my case where condition at "option.store.code" level then what should i have to do?? Commented Sep 23, 2015 at 8:55
  • @AsheshKhatri have you tried by using find and modify? Commented Sep 23, 2015 at 9:49

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.