gpt4 book ai didi

c# - 更新多级嵌入式mongo数组

转载 作者:可可西里 更新时间:2023-11-01 09:46:18 25 4
gpt4 key购买 nike

我无法找到正确的语法来替换嵌套在集合中多个层次的对象数组。我的偏好是只更新单个属性,但自从阅读下面的链接后,似乎替换整个数组是最好的选择。

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);

那么使我能够更新 Pets 数组中的详细信息的最佳过程是什么?

最佳答案

这是您要查找的代码:您需要将 BsonArray 传递给 Update.Set 值。要创建该数组,您需要将每个“宠物”包装在 BsonDocumentWrapper 中,以便序列化库知道如何适本地序列化它们。

var query = new QueryDocument { { "Children._id", new ObjectId("4f979621682dbc1a8cefecaf") } };
var petDocuments = BsonDocumentWrapper.CreateMultiple(pets);
var petArray = new BsonArray(petDocuments);
var update = Update.Set("Children.$.Pets", petArray);
parents.Update(query, update);

关于c# - 更新多级嵌入式mongo数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11120439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com