1

Want to update array of object field from existing object field value in mongo.. following is my collection..

{
    "_id": ObjectId(),
    "sku": "V4696-DR-V33",
    "options": [
        {
            "sku": "8903689984338",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 8
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 10
                }
            ]
        },
        {
            "sku": "1742564789",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 4
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 6
                }
            ]
        },
    ]
}

from above collection want to update inventory of above collection where options.retailer.id = 101011 then update options.retailer.inventory by difference between options.retailer.inventory & options.retailer.buffer_quantity then output like show as below

{
    "_id": ObjectId(),
    "sku": "V4696-DR-V33",
    "options": [
        {
            "sku": "8903689984338",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 7
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 10
                }
            ]
        },
        {
            "sku": "1742564789",
            "retailer": [
                {
                    "buffer_quantity": 1,
                    "id" : 101011,
                    "inventory": 3
                },
                {
                    "buffer_quantity": 2,
                    "id" : 101012,
                    "inventory": 6
                }
            ]
        },
    ]
}
Ashesh Khatri
  • 162
  • 2
  • 11
  • Possible duplicate of [Update MongoDB field using value of another field](http://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) – Blakes Seven Mar 04 '16 at 05:34
  • want to update array of specific object value.. not to update all retailer inventory data..for that i'm getting stuck.. – Ashesh Khatri Mar 04 '16 at 05:36
  • And what the duplicate question and answers tell you is that you need to read in the document in order to get the values and update one document at a time ( or specifically in your case, you really should be doing one array element per document at a time ). There is no other way to do this. You need to read the document(s) first. – Blakes Seven Mar 04 '16 at 05:39

0 Answers0