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.

Ashesh Khatri
  • 162
  • 2
  • 11
  • possible duplicate of [How to change all the array elements in a mongodb document to a certain value?](http://stackoverflow.com/questions/32610663/how-to-change-all-the-array-elements-in-a-mongodb-document-to-a-certain-value) – styvane Sep 23 '15 at 08:00
  • I wan't mongo query to direct update to my All Inventory value. U have any idea about this??? – Ashesh Khatri Sep 23 '15 at 08:39
  • The duplicate shows how you can do it. That is all.. – styvane Sep 23 '15 at 08:48
  • In my case where condition at "option.store.code" level then what should i have to do?? – Ashesh Khatri Sep 23 '15 at 08:55
  • @AsheshKhatri have you tried by using [find and modify](https://docs.mongodb.org/v3.0/reference/method/db.collection.findAndModify/#db.collection.findAndModify)? – Smart003 Sep 23 '15 at 09:49
  • @Smart003 wrong! findAndModify has nothing to do here. – styvane Sep 23 '15 at 10:04
  • @AsheshKhatri is it so difficult to understand how the duplicate answer works? – styvane Sep 23 '15 at 10:05
  • @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. – Ashesh Khatri Sep 23 '15 at 10:07
  • @AsheshKhatri see the below answer – Smart003 Sep 23 '15 at 10:09

1 Answers1

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.

Smart003
  • 1,119
  • 2
  • 16
  • 31
  • 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 } ], }, ] } – Ashesh Khatri Sep 23 '15 at 10:20
  • @AsheshKhatri as per the above query all the documents which matches the conditions will be update. – Smart003 Sep 23 '15 at 10:24
  • 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 – Smart003 Sep 23 '15 at 10:26
  • 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 ?? – Ashesh Khatri Sep 23 '15 at 10:31
  • can you give the entire structure of your collection. if i know the collection structure i can help on this – Smart003 Sep 23 '15 at 10:34
  • How do i share with u ?? – Ashesh Khatri Sep 23 '15 at 10:39
  • 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":}} – Smart003 Sep 23 '15 at 11:05
  • ya this can work but this i m looking for direct update using mongo query – Ashesh Khatri Sep 23 '15 at 11:39