在我的MongoDB中,我有Document,如下所示。
如何更新内部文档结构中的文档如下所示:[{1},[{2}],[{3}],[{4}]]
。
Ex:子子文档中的子文档包含objectId:1
和objectId:2
以及objectId:3
和objectId:4
。
像:
[{ _id : ObjectId("1")}],
[{ _id : ObjectId("2")}],
[{ _id : ObjectId("3")}],
[{ _id : ObjectId("4")}]
"Child": [{
"_id": ObjectId("1"),
Name: "Raghu",
Age : 21,
"subchild": [{
"_id": ObjectId("2"),
"Name": "Yuva",
Age : 23,
},
[{
"_id": ObjectId("3"),
Name: "Ravi",
Age : 25
}
],
[{
"_id": ObjectId("4"),
"Name": "Ram",
Age : 27
}
]
],
}
如何更新以下子子文档特定_Id:ObjectId(“3”)和_Id:ObjectId(“4”) 例如:[{“_ id”:ObjectId(“4”),“名称”:“Ram”,}]
我在C#中试过这样:
I updated ObjectId : 1 like Example child.$.Subchild.0.Name="Raghu"
But not Updated ObjectId : 4 like Example child.$.Subchild.3.Name="Ram"
MongoServerSettings settings = new MongoServerSettings();
settings.Server = new MongoServerAddress("127.1.1.1", 27017);
MongoServer servers = new MongoServer(settings);
var db = servers.GetDatabase("StudentDB");
MongoCollection<Student> pax;
pax = db.GetCollection<Student>("StudentDetails");
IMongoQuery queryEdit = Query.EQ("Child.subchild._id", new ObjectId("4"));
var updateValues = new List<UpdateBuilder>();
{ updateValues.Add(Update.Set("child.$.Subchild.3.Name", "Ramu")); }
updateValues.Add(Update.Set("child.$.subchild.3.Age", 27));
IMongoUpdate update = Update.Combine(updateValues);
SafeModeResult sm = pax.Update(queryEdit, update, UpdateFlags.None, SafeMode.True);
答案 0 :(得分:0)
child.$.Subchild.0.Name="Raghu"
不需要对第一个数组游标的引用,需要MongodDB的第二个游标child.Subchild.$.Name="Raghu"
的引用不要更新两个引用“$”
db.foo.update({_id:ObjectId(...),"child.Subchild.Name":"Ram"},{$set:{child.Subchild.$.Name="Raghu"}});