更新子嵌套文档Mongodb中的第n个元素

时间:2014-02-14 16:47:11

标签: mongodb nested document updates

我需要更新子嵌套文档中的第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"
        }
 });

感谢您的帮助!!!

1 个答案:

答案 0 :(得分:0)

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

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