想要在Mongo中更新数组对象值的数组 [英] Want to Update Array of Array Object Value in Mongo
问题描述
我有以下MongoDB文档:
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"
},
]
}
我想更新代码值为"AND1"的每个库存值. 我想在mongo查询或任何python脚本中查询以嵌套值更新整个文档. 我对此问题感到非常困惑.
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.
推荐答案
尝试以下查询以进行更新
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....
我以您的数据为例
现在我已经使用了上面的查询 db.stack.update({"options.0.stores":{$ elemMatch:{code:"AND1"}}},{$ set:{"options.0.stores.$.inventory":200}})
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
我已将库存更新为200,其中代码为:"AND1"
i had update the inventory to 200 where code:"AND1"
查看更改.
这篇关于想要在Mongo中更新数组对象值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!