我使用spring-data-mongo mongoTemplate更新嵌入的文档。 以下是我的数据模型:
{
"_id":"3KNAM390aENt",
"field":"test001",
"name":"001",
"sort":0,
"embedFields1":{
"_id":"1",
"field":"field1",
"name":"name1",
"limit":1,
"canBeChange":true,
"needAudit":false,
"embedFields2":{
"_id":"22",
"field":"field22",
"name":"name22",
"limit":1,
"canBeChange":true,
"needAudit":false,
"embedFields3":{
"_id":"3",
"field":"field3",
"name":"name3",
"limit":1,
"canBeChange":true,
"needAudit":false,
"embedFields4":{
"_id":"4",
"field":"field4",
"name":"name4",
"limit":1,
"canBeChange":true,
"needAudit":false,
"embedFields5":{
"_id":"5",
"field":"field5",
"name":"name5",
"limit":1,
"canBeChange":true,
"needAudit":false,
"embedFields6":{
"_id":"66",
"field":"field66",
"name":"name66",
"limit":1,
"canBeChange":true,
"needAudit":false
}
}
}
}
}
}
}
我知道如何使用$更新首先嵌入的embedFields1, 但是如何更新embedFields2,embedFields3,embedFields4 .......?
答案 0 :(得分:0)
要浏览您的结构,可以使用如下语法:
“ firstlevel.secondlevel.thirdlevel
”
这意味着您可以创建动态逻辑来创建该字符串并在mongodb命令中使用它。
示例
查找
db.your_collection.find({"embedFields1.embedFields2.embedFields3._id" : "2"})
更新
db.your_collection.update({"embedFields1.embedFields2.embedFields3._id" : "3"} , {$set : {"embedFields1.embedFields2.embedFields3.name" : "test"}})