【问题标题】:Mongo update to many queryMongo 更新到许多查询
【发布时间】:2022-01-01 04:21:57
【问题描述】:

有谁知道如何在 mongo 中更改属性值 来自

我将 JSON.units[0].services[0].label 从 etiket controle 更改为 AuditLabel

我试过这个:

db.getCollection('assortiment').updateMany({"units.$[].services.$[].label": "etiket controle"},{ "$set": { "units.$[].services.$[].label": "AuditLabel" }})

但没有成功,因为它没有选择任何东西

{
    "_id" : "764",
    "meta" : {
        "groupsId" : "764",
        "type" : "DRYGR"
    },
    "units" : [ 
        {
            "unit" : "BASE_UNIT_OR_EACH",
            "gtin" : "08711728556206",
            "services" : [ 
                {
                    "label" : "etiket controle",
                    "collection" : "gtins"
                }
            ]
        }
    ]
}

{
    "_id" : "764",
    "meta" : {
        "groupsId" : "764",
        "type" : "DRYGR"
    },
    "units" : [ 
        {
            "unit" : "BASE_UNIT_OR_EACH",
            "gtin" : "08711728556206",
            "services" : [ 
                {
                    "label" : "AuditLabel",
                    "collection" : "gtins"
                }
            ]
        }
    ]
}

【问题讨论】:

  • 如果您不需要搜索,并且您已经知道JSON.units[0].services[0].label 之类的索引,您可以执行this 之类的操作,如果您想在任何情况下更新它,您可以删除搜索,但是我想你不想要这个,你想在任何地方更新标签,但不确定

标签: node.js mongodb


【解决方案1】:

试试这个代码,它对我有用

db.getCollection('assortiment').updateMany(
  {
    "units.services.label": "etiket controle"
  },
  {
    "$set":
      { "units.$[].services.$[].label": "AuditLabel" }
  }
)

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多