c# - Mongodb C# - 仅当嵌套数组的嵌套元素匹配条件时才聚合 $push
问题描述
在问之前我已经在互联网上看了很多,但我找不到解决方案......
有一个我的类和对象的例子:
课程:
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 是 A 集合的一部分,没有 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();
所以我想要实现的是检索所有对象A,并过滤所有B对象以仅获取Type等于0的对象,并且对于每个对象B,我应该拥有Person,例如:
{
"_id": 1;
"Type", 1,
"ListOfB": [
{
"_id": 1,
"PersonId": 102,
"Type": 0,
"Person": { "_id": 102, "Name": "Billy" }
}
]
},
{
"_id": 2;
"Type", 1,
"ListOfB": [
{
"_id": 1,
"PersonId": 89,
"Type": 0,
"Person": { "_id": 89, "Name": "Finn" }
},
{
"_id": 2,
"PersonId": 404,
"Type": 0,
"Person": { "_id": 404, "Name": "Jake" }
}
]
}
但这不起作用......我可以检索类型 1 的每个 A 对象,以及它们的 B 对象中的所有 Person 对象,但我得到了 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();
推荐阅读
- java - 为旧 Android 设备上的应用程序使用最新的库
- ios - 更新 GoogleTagManager Pod
- sql - 如何为从 SQL 中的两个日期列中提取的日期范围生成所有日期
- c - 免费 rtos 中 xTaskAbortDelay 函数的用途是什么?
- php - 本地文件上带有 html 的 PHP
- c# - 使用 Gridview asp.net 从 SQL Server 读取数据
- r - R中的客户流失标签
- android - 此项目的 Gradle 设置尚未配置 - android studio
- pandas - 当我从 pandas Dataframe 中选择系列时,reshape 是不推荐使用的问题
- android - 如何根据日期和时间对 Array-List 中的未来事件进行排序