【问题标题】:Mongodb: update array of arrayMongodb:更新数组数组
【发布时间】:2015-11-28 19:47:40
【问题描述】:

我有一个名为 products 的集合,我想在其中更新 options.stores.code 下有空字段的代码 结构如下:-

{
"_id": ObjectId(),
"sku": "V4696-DR-V33",
"options": [
    {
        "sku": "8903689984338",
        "stores": [
            {
                "code": "AND1",
                "zipcode": "110070",
                "inventory": -5000
            },
            {
                "code": "AND2",
                "zipcode": "201010",
                "inventory": -5000
            },
            {
                "code": "AND3",
                "zipcode": "411001",
                "inventory": -5000
            },
            {
                "code": "AND4",
                "zipcode": " 700020",
                "inventory": -5000
            }
        ],
    },
    {
        "sku": "1742564789",
        "stores": [
            {
                "code": "AND1",
                "zipcode": "110070",
                "inventory": -5000
            },
            {
                "code": "AND2",
                "zipcode": "201010",
                "inventory": -5000
            },
            {
                "code": "AND3",
                "zipcode": "411001",
                "inventory": -5000
            },
            {
                "code": "AND8",
                "zipcode": " 700020",
                "inventory": -5000
            }
        ],
    },

]

}

想要更新具有相同代码库存

【问题讨论】:

  • 你可以在这里找到答案link
  • 感谢您的回复。我已经检查了查询仅适用于数组中的单个对象。但不适用于所有匹配的元素。

标签: mongodb mongodb-query


【解决方案1】:

用于更新空商店代码:

db.products.update({"code":""},{$set:{"code":"new value"}}

【讨论】:

  • 这是正常的更新,但不会像我期望的那样。
【解决方案2】:

使用python脚本对此更新使用任何循环............并进行了轻微修改------{$set:{"options.(loop variable).stores. $.inventory":}} 这可以帮助查询

【讨论】:

  • 我们可以在不使用任何循环的情况下使用 mongo 查询吗,因为我有很多记录要更新。
  • 使用 mongo 查询没有直接更新的方法,你必须使用任何循环。
  • k 感谢将在此解决方案上工作并将更新发布给您:) 谢谢
猜你喜欢
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2021-12-24
  • 2016-02-09
相关资源
最近更新 更多