[英]How can I remove an item from an array nested in another array with Mongo/C#
I've found plenty of examples showing how to remove an item from an array within the root of a collection of documents and I can successfully add items to nested arrays.我发现了很多示例,展示了如何从文档集合的根中的数组中删除一个项目,并且我可以成功地将项目添加到嵌套的 arrays。 But I can't seem to make headway on removing or updating an item in a nested array.
但我似乎无法在删除或更新嵌套数组中的项目方面取得进展。
An example would be where I have a collection of groups.一个例子是我有一组组。 In that group, there is an array(list) of Members.
在该组中,有一个成员数组(列表)。 Each of those members could have multiple nicknames that they go by.
这些成员中的每一个都可以有多个昵称,他们是 go。 I need to know how to go about deleting/updating a Nickname for a specific member.
我需要知道如何删除/更新特定成员的昵称。
Ex:前任:
// example mongo data
[{
"_id": 482232389408781300,
"Name": "Group A",
"Members": [
{
"_id": "X4cLx72J9",
"Name": "John Smith",
"NickNames: [
{
"_id": "V2889Jw8",
"Name": "Smithy"
},
{
"_id": "V82lvi2",
"Name": "Ol John"
}
]
},
{
"_id": "X4c7872J9",
"Name": "Robert James",
"NickNames: [
{
"_id": "V2Bl9Jw8",
"Name": "Not Rick James"
},
{
"_id": "V8Qrvi2",
"Name": "Slick"
}
]
}
}
]
// c# classes
class Group{
public ulong Id {get;set;} // Unique Id
public string Name {get;set;}
public List<Member> Members{get;set;}
}
class Member{
public string Id {get;set;} // Unique Id
public string Name {get;set;}
public List<NickName> NickNames{get;set;}
}
public NickName{
public string Id {get;set;} // Unique Id
public string Name {get;set;}
}
Editing for clarification I'm needing to know how to complete this request using the MongoDB driver for C#编辑澄清我需要知道如何使用 C# 的 MongoDB 驱动程序来完成此请求
var guilds = db.GetCollection<Group>("groups");
var filter = Builders<Group>.Filter;
/// At this point I'm unsure how the filter/update values should be coded in order to update the mongo db.
Assuming this is your data,假设这是您的数据,
List<NickName> nickNameList = new List<NickName>();
nickNameList.Add(new NickName() { Id = "V2889Jw8", Name = "Smithy" });
nickNameList.Add(new NickName() { Id = "V82lvi2", Name = "Ol John" });
List<NickName> nickNameList2 = new List<NickName>();
nickNameList2.Add(new NickName() { Id = "V2Bl9Jw8", Name = "Not Rick James" });
nickNameList2.Add(new NickName() { Id = "V8Qrvi2", Name = "Slick" });
List<Member> members = new List<Member>();
members.Add(new Member() { Id = "X4cLx72J9", Name = "John Smith", NickNames = nickNameList });
members.Add(new Member() { Id = "X4c7872J9", Name = "Robert James", NickNames = nickNameList2 });
List<Group> group = new List<Group>();
group.Add(new Group(){
Id = 482232389408781300,
Name = "Group A",
Members = members
});
We can use the .Find()
method of List
to find the specific record in the list and update or delete it.我们可以使用
List
的.Find()
方法来查找列表中的特定记录并更新或删除它。
For example:例如:
//Update
group.Find(x => x.Id == 482232389408781300).Members.Find(x => x.Id == "X4c7872J9").NickNames.Find(n => n.Name == "Not Rick James").Name = "Rick";
//Delete
List<NickName> nickNames = group.Find(x => x.Id == 482232389408781300).Members.Find(x => x.Id == "X4cLx72J9").NickNames;
nickNames.Remove(nickNames.Find(n => n.Name == "Smithy"));
I think I've worked out two methods that work for me.我想我已经找到了两种对我有用的方法。 They might not be the most efficient so I will continue reading more into.
它们可能不是最有效的,所以我将继续阅读更多内容。 But here is what I have thus far.
但这是我到目前为止所拥有的。
The following deletion method uses the positional all '$[]' option as each Nickname has a unique ID.以下删除方法使用位置 all '$[]' 选项,因为每个昵称都有唯一的 ID。
// Delete An array item
var groupId = 482232389408781300;
var memberId = "X4cLx72J9";
var nickId = "V2889Jw8";
var collection = _db.GetCollection<Group>("groups");
var filter = Builders<Group>.Filter.Eq(group => group.Id, groupId);
var update = Builders<Group>.Update.PullFilter("Members.$[].NickNames", Builders<BsonDocument>.Filter.Eq("_id", nickId));
collection.UpdateOne(filter, update);
The update method is using the ArrayFilters option.更新方法使用 ArrayFilters 选项。 I imagine you could use this for the deletion method as well.
我想您也可以将其用于删除方法。
// Update An array item
var groupId = 482232389408781300;
var memberId = "X4cLx72J9";
var nickId = "V2889Jw8";
var newNick = "Loon";
var collection = _db.GetCollection<Group>("groups");
var filter = Builders<Group>.Filter.Eq(group => group.Id, groupId);
var update = Builders<Group>.Update.Set("Members.$[m].NickNames.$[n].Name", newNick);
var filterArray = new List<ArrayFilterDefinition>{
new JsonArrayFilterDefinition<Group>(string.Format("{{'m._id': '{0}'}}", memberId)),
new JsonArrayFilterDefinition<Group>(string.Format("{{'n._id': '{0}'}}", nickId))
};
var updateOptions = new UpdateOptions { ArrayFilters = filterArray };
collection.UpdateOne(filter, update, updateOptions);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.