我正在寻找解决方案,我可以更新深层嵌套的mongodb文档, 我尝试使用$ sign,但它又有一个限制,它只能使用一次,这对原因没有帮助。那么,我有什么方法可以做到这一点。 我的文件看起来像
{
"_id" : ObjectId("56fa1a0d8c54754a27ab7a50"),
"name" : "A",
"location" : [
{
"state" : "MH",
"city" : [
{
"cityID" : 11,
"cityName" : "nashik"
},
{
"cityID" : 12,
"cityName" : "mumbai"
},
{
"cityID" : 13,
"cityName" : "pune"
}
]
},
{
"state" : "GJ",
"city" : [
{
"cityID" : 21,
"cityName" : "gandhiNagar"
},
{
"cityID" : 22,
"cityName" : "surat"
},
{
"cityID" : 23,
"cityName" : "ahemdabad"
}
]
}
]
}
,
{
"_id" : ObjectId("56fa1ab88c54754a27ab7a51"),
"name" : "B",
"location" : [
{
"state" : "MH",
"city" : [
{
"cityID" : 11,
"cityName" : "nashik"
},
{
"cityID" : 12,
"cityName" : "mumbai"
},
{
"cityID" : 13,
"cityName" : "pune"
}
]
},
{
"state" : "GJ",
"city" : [
{
"cityID" : 21,
"cityName" : "gandhiNagar"
},
{
"cityID" : 22,
"cityName" : "surat"
},
{
"cityID" : 23,
"cityName" : "ahemdabad"
}
]
}
]
}
,
{
"_id" : ObjectId("56fa1ac98c54754a27ab7a52"),
"name" : "A",
"location" : [
{
"state" : "MH",
"city" : [
{
"cityID" : 11,
"cityName" : "nashik"
},
{
"cityID" : 12,
"cityName" : "mumbai"
},
{
"cityID" : 13,
"cityName" : "pune"
}
]
},
{
"state" : "GJ",
"city" : [
{
"cityID" : 21,
"cityName" : "gandhiNagar"
},
{
"cityID" : 22,
"cityName" : "surat"
},
{
"cityID" : 23,
"cityName" : "ahemdabad"
}
]
}
]
}
所以现在我想更新字段cityName,其中名称:“A”和cityID:23
为此我写了一个查询,但它有静态索引,
db.LevelTest.update({"name":"A","location.city.cityID":23},{$set:{"location.$.city.2.cityName":"newCity"}},false,true)
那么有什么方法可以编写通用查询而不是提供数组元素的静态索引,或任何其他方法, 或改变结构是唯一的解决方案?