问题
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.
回答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....
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.
来源:https://stackoverflow.com/questions/32733790/want-to-update-array-of-array-object-value-in-mongo