Mongodb C#-仅当嵌套数组的嵌套元素与条件匹配时,聚合$push
在询问之前,我已经在网上查阅了很多,但我找不到解决方案 下面是我的类和对象的一个示例: 课程:Mongodb C#-仅当嵌套数组的嵌套元素与条件匹配时,聚合$push,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,在询问之前,我已经在网上查阅了很多,但我找不到解决方案 下面是我的类和对象的一个示例: 课程: public class A { [BsonId] public int _id {get; set;} public List<B> ListOfB {get; set;} public TypeA Type {get; set;} } public class B { public int _id {get; set;} public
public class A {
[BsonId]
public int _id {get; set;}
public List<B> ListOfB {get; set;}
public TypeA Type {get; set;}
}
public class B {
public int _id {get; set;}
public int PersonId {get; set;}
public Person Person {get; set;} // This one is not save on the DB, it is retrieved through a Lookup
public int Type {get; set;}
}
public class Person {
[BsonId]
public int _id {get; set;}
public string Name {get; set;}
}
{
"_id": 0;
"Type", 0,
"ListOfB": [
{
"_id": 0,
"PersonId": 300,
"Type": 0
},
{
"_id": 1,
"PersonId": 24,
"Type": 0
},
{
"_id": 2,
"PersonId": 59,
"Type": 1
}
]
},
{
"_id": 1;
"Type", 1,
"ListOfB": [
{
"_id": 0,
"PersonId": 45,
"Type": 1
},
{
"_id": 1,
"PersonId": 102,
"Type": 0
},
{
"_id": 2,
"PersonId": 33,
"Type": 1
}
]
}
,
{
"_id": 2;
"Type", 1,
"ListOfB": [
{
"_id": 0,
"PersonId": 66,
"Type": 1
},
{
"_id": 1,
"PersonId": 89,
"Type": 0
},
{
"_id": 2,
"PersonId": 404,
"Type": 0
}
]
}
B是集合的一部分,不存在B的集合。
到目前为止,我已经做了类似的事情来检索对象:
BsonDocument groupMappingA = new BsonDocument();
groupMappingA.Add(new BsonElement("_id", "$_id"));
BsonDocument groupMappingB = new BsonDocument();
groupMappingB.Add(new BsonElement("_id", "$ListOfB._id"));
groupMappingB.Add(new BsonElement("PersonId", "$ListOfB.PersonId"));
groupMappingB.Add(new BsonElement("Person", "$ListOfB.Person"));
groupMappingB.Add(new BsonElement("Type", "$ListOfB.Type"));
groupMappingA.Add(new BsonElement("ListOfB", new BsonDocument(
"$push", new BsonDocument(
"$cond", new BsonArray {
new BsonDocument("$eq", new BsonDocument("$ListOfB.Type", "0")),
new BsonDocument("$ListOfB", groupMappingB)
}
)
)
)
);
AggregateUnwindOptions<A> unwindOptions = new AggregateUnwindOptions<A>() { PreserveNullAndEmptyArrays = true };
db.GetCollection<A>("A").Aggregate()
.match(x => x.Type == 1)
.Unwind("ListOfB", unwindOptions)
.Lookup("Person", "ListOfB.PersonId", "_id", "ListOfB.Person") // Here we set up Person through the ID saved in the object B
.Unwind("ListOfB.Person", unwindOptions)
.Group(groupMappingA)
.As<A>()
.ToList();
但这不起作用。。。我可以检索类型为1的每个A对象,所有的Person对象都在B对象中,但我得到了B的完整列表,而不仅仅是类型为0的
我试着先在Mongodb Compass上做,但到目前为止,我没有成功,然后我在互联网上查找,但我找不到我要找的。。。我开始怀疑是否有可能在嵌套列表中获得带有过滤器的对象
提前感谢您的帮助。我找到了解决方法,我将在这里发布答案,以备有人需要
BsonDocument groupMapping = new BsonDocument();
groupMapping.Add(new BsonElement("_id", new BsonDocument("$first", "$_id")));
groupMapping.Add(new BsonElement("ListOfB", new BsonDocument("$push", "$ListOfB")));
AggregateUnwindOptions<A> unwindOptions = new AggregateUnwindOptions<A>() { PreserveNullAndEmptyArrays = true };
db.GetCollection<A>("A").Aggregate()
.match(x => x.Type == 1)
.Unwind("ListOfB", unwindOptions)
.Match(new BsonDocument("ListOfB.Type", 0)) // Just put a match right after the Unwind, you'll be able to filter the elements of the list as you want.
.Lookup("Person", "ListOfB.PersonId", "_id", "ListOfB.Person")
.Unwind("ListOfB.Person", unwindOptions)
.Group(groupMapping)
.As<A>()
.ToList();
bsondocumentgroupmapping=newbsondocument();
添加(新的bsonement(“\u id”,新的BsonDocument(“$first”,“$\u id”));
添加(新的bsonement(“ListOfB”,新的BsonDocument(“$push”,“$ListOfB”));
AggregateUnwindOptions unwindOptions=new AggregateUnwindOptions(){preserveNullandmptyarray=true};
db.GetCollection(“A”).Aggregate()
.match(x=>x.Type==1)
.展开(“ListOfB”,展开选项)
.Match(new BsonDocument(“ListOfB.Type”,0))//只要在展开后放置一个匹配项,就可以根据需要筛选列表中的元素。
.Lookup(“Person”、“ListOfB.PersonId”、“id”、“ListOfB.PersonId”)
.Unwind(“ListOfB.Person”,取消选择)
.Group(组映射)
.As()
.ToList();
BsonDocument groupMapping = new BsonDocument();
groupMapping.Add(new BsonElement("_id", new BsonDocument("$first", "$_id")));
groupMapping.Add(new BsonElement("ListOfB", new BsonDocument("$push", "$ListOfB")));
AggregateUnwindOptions<A> unwindOptions = new AggregateUnwindOptions<A>() { PreserveNullAndEmptyArrays = true };
db.GetCollection<A>("A").Aggregate()
.match(x => x.Type == 1)
.Unwind("ListOfB", unwindOptions)
.Match(new BsonDocument("ListOfB.Type", 0)) // Just put a match right after the Unwind, you'll be able to filter the elements of the list as you want.
.Lookup("Person", "ListOfB.PersonId", "_id", "ListOfB.Person")
.Unwind("ListOfB.Person", unwindOptions)
.Group(groupMapping)
.As<A>()
.ToList();