【问题标题】:Update a nth element in a sub nested document Mongodb更新子嵌套文档 Mongodb 中的第 n 个元素
【发布时间】:2014-02-15 01:23:24
【问题描述】:

我需要更新子嵌套文档中的第 n 个元素:我有这个结构:

{
  "_id" : ObjectId("52fdbbb470c68c3c14d60700"),
  "curations" : [{

      "measurements" : 
      [
        {
          "_id" : ObjectId("52fdbbb470c68c3c14d80700"),
          "name" : "GSM15729",
          "Vals" : ["N/A", "bronchus, lung", "bronchial epithelial cell", "N/A", "N/A", "N/A", "non-smoker", "GPL96"]
        }, 
        {
          "_id" : ObjectId("52fdbbb470c68c3c14d90700"),
          "name" : "GSM104072",
          "Vals" : ["N/A", "bronchus, lung", "bronchial epithelial cell", "N/A", "N/A", "N/A", "current smoker", "GPL96"]
        }

        ]

    }]
}

例如,我想更新最后一个子数组“Vals”中的第 4 个“N/A”(第 5 个位置)

我用命令行尝试了这段代码,但它似乎不起作用

db.Series.find({"curations.measurements._id":new ObjectId("52fdbbb470c68c3c14d90700")},
    { $set: {
        "curations.measurements.$.Vals.4": "new_value"
        }
 });

感谢您的帮助!!!

【问题讨论】:

    标签: mongodb nested document updates


    【解决方案1】:

    最后我发现了错误,只是我忘记了一个零......(数组)

    db.Series.find({"curations.0.measurements._id":new ObjectId("52fdbbb470c68c3c14d90700")},
        { $set: {
            "curations.0.measurements.$.Vals.4": "new_value" <=In this line
            }
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2014-09-23
      • 2018-12-25
      • 1970-01-01
      • 2013-08-12
      • 2016-08-04
      相关资源
      最近更新 更多