我無法找到正確的語法來替換嵌套在集合內幾個級別深度的對象數組。我的偏好是更新個別屬性,但是從閱讀下面的鏈接看來,替換整個陣列是最好的選擇。更新多級嵌入式mongo陣列
https://jira.mongodb.org/browse/SERVER-831
所以,我有以下類爲例:
public class Parent
{
public ObjectId _id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public Collection<Child> Children { get; set; }
}
public class Child
{
public ObjectId _id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public Collection<Pet> Pets { get; set; }
}
public class Pet
{
public string Name { get; set; }
}
使用下面的代碼創建一個家長,添加一些兒童,並添加一個寵物的孩子之一。
// Construct Objects
Parent parent = new Parent() { _id = new ObjectId("4f979621682dbc1a8cefecb1") };
Collection<Child> children = new Collection<Child>();
Collection<Pet> pets = new Collection<Pet>();
children.Add(new Child()
{ _id = new ObjectId("4f979621682dbc1a8cefecaf"),
Firstname = "Child",
Lastname = "One" });
children.Add(new Child()
{ _id = new ObjectId("4f979621682dbc1a8cefecb0"),
Firstname = "Child",
Lastname = "Two" });
pets.Add(new Pet() { Name = "Fishy" });
parent.Children = children;
parent.Children[0].Pets = pets;
// Connect to Mongo
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var db = server.GetDatabase("test");
// Insert into parent collection
MongoCollection<Parent> parents;
parents = db.GetCollection<Parent>("parents");
parents.Insert<Parent>(parent, MongoDB.Driver.SafeMode.True);
這成功地插入的對象,生成以下JSON結果:
{ "_id" : ObjectId("4f979621682dbc1a8cefecb1"),
"Firstname" : null,
"Lastname" : null,
"Children" :
[
{
"_id" : ObjectId("4f979621682dbc1a8cefecaf"),
"Firstname" : "Child",
"Lastname" : "One",
"Pets" :
[
{
"Name" : "Fishy"
}
]
},
{
"_id" : ObjectId("4f979621682dbc1a8cefecb0"),
"Firstname" : "Child",
"Lastname" : "Two",
"Pets" : null
}
]
}
更新個人文檔元素也似乎是一個簡單的過程,並用下面的代碼成功地起作用。
// Change children's name
var query = new QueryDocument { { "Children._id", new ObjectId("4f979621682dbc1a8cefecaf") } };
var update = Update.Set("Children.$.Firstname", "Something");
parents.Update(query, update);
現在我不能解決的問題是如何替換Pets數組。下面的代碼不會編譯爲Update.Set不接受集合。
// Change pets information
pets[0].Name = "Fishy2"; // change to pet
pets.Add(new Pet() { Name = "Doggy" }); // add new pet
query = new QueryDocument { { "Children._id", new ObjectId("4f979621682dbc1a8cefecaf") } };
update = Update.Set("Children.$.Pets", pets);
parents.Update(query, update);
那麼什麼是最好的過程,可以讓我更新寵物數組中的細節?
。使用通用集合將會更容易! –
@Rhhound:他正在使用一個通用集合。你指的是什麼? –