在我的 MongoDB 中,我有 Document,如下所示。如何更新内部文档结构中的文档如下所示:[{1},[{2}],[{3}],[{4}]]
.
例如:子文档中的子文档有objectId:1
andobjectId:2
和objectId:3
and 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”),“Name”:“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);