想要更新Mongo中的数组对象值数组

时间:2015-09-23 07:44:07

标签: mongodb multidimensional-array mongoose mongodb-query pymongo

我有以下MongoDB文档:

{
"_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脚本中查询以使用嵌套值更新整个文档。 我非常担心这个问题。

1 个答案:

答案 0 :(得分:1)

尝试以下查询以进行更新

db.stack.update({"options.0.stores":{$elemMatch:{code:"AND1"}}},{$set:{"options.0.stores.$.inventory":200}})

检查我是否会用图片更新.... enter image description here

我把你的数据作为例子

现在我使用了上面的查询  db.stack.update({" options.0.stores" {$ elemMatch:{代码:" AND1"}}},{$设置:{"选项。 。0.stores $库存":200}})

然后结果显示在下图中 enter image description here

我已将库存更新为200,代码为:" AND1"

查看更改。