gpt4 book ai didi

c# - 条件聚合器的 MongoDb.Linq 等效查询

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

我按以下方式使用 MongoDb.Linq 对我的数据进行分组:

group r1 by new { r1.SupId, r1.SupFullName } into g2
select new
{
g2.Key.SupFullName,
Volunteers = from v0 in g2
where v0.VolId != g2.Key.SupId
select new { v0.VolFullName, v0.VolPeopleCount }
}

也就是说,我只希望在 Volunteers 中那些 Id 与组 key 不同的人。我期待驱动程序生成一个条件 $push,但它给出了一个简单的:

"$push" : { 
"VolFullName" : "$VolFullName",
"VolPeopleCount" : "$VolPeopleCount"
}

现在,通过阅读这篇 post我能够将上述查询转换为以下查询:

"$push": {
"$cond": [
{ "$ne": [ "$VolId", "$SupId" ] },
{
"VolFullName": "$VolFullName",
"VolPeopleCount": "$VolPeopleCount"
},
null
]
}

这给了我想要的结果。我想知道如何(如果可能的话)指示 csharp 驱动程序生成如上所述的查询。提前致谢!

编辑:按照@mickl 的建议,我能够生成一个有条件的$push(还有一个有条件的$sum)但是结果不如预期。假设我的收藏中有以下文档:

{ N: "P_1", S: { I: "S_1", FN: "S_1" }, C: { I: "S_1", FN: "S_1" } }
{ N: "P_2", S: { I: "S_1", FN: "S_1" }, C: { I: "S_1", FN: "S_1" } }
{ N: "P_3", S: { I: "S_1", FN: "S_1" }, C: { I: "S_1", FN: "S_1" } }
{ N: "P_4", S: { I: "S_1", FN: "S_1" }, C: { I: "C_2", FN: "C_2" } }
{ N: "P_5", S: { I: "S_1", FN: "S_1" }, C: { I: "C_2", FN: "C_2" } }
{ N: "P_6", S: { I: "S_1", FN: "S_1" }, C: { I: "C_2", FN: "C_2" } }
{ N: "P_7", S: { I: "S_1", FN: "S_1" }, C: { I: "C_3", FN: "C_3" } }
{ N: "P_8", S: { I: "S_1", FN: "S_1" }, C: { I: "C_3", FN: "C_3" } }
{ N: "P_9", S: { I: "S_1", FN: "S_1" }, C: { I: "C_3", FN: "C_3" } }

这里的N, I, FN, S and C 表示名字, id, 全名, supervisor 和 creator 分别。如您所见,主管 S_1 正在监督每个文档,创建了 3 个文档,另外 2 个创建者创建了 6 个文档,它们也由 S_1 监督。我想做的是由主管总结这些文档,然后由创建者总结文档(人)的数量。所以,我的 LINQ 查询如下所示:

var q = from p in col.AsQueryable()
group p by new
{
SupFullName = p.S.FN,
SupId = p.S.I,
CtorFullName = p.C.FN,
CtorId = p.C.I
} into g1
select new
{
g1.Key.SupFullName,
g1.Key.SupId,
VolFullName = g1.Key.CtorFullName,
VolId = g1.Key.CtorId,
VolPeopleCount = g1.LongCount()
} into r1
group r1 by new { r1.SupId, r1.SupFullName } into g2
select new
{
g2.Key.SupFullName,
g2.Key.SupId,
Volunteers = g2.Select(v => v.VolId != g2.Key.SupId
? new { v.VolId, v.VolFullName, v.VolPeopleCount }
: null),
SupPeopleCount = g2.Select(v => v.VolId == g2.Key.SupId
? v.VolPeopleCount
: 0).Sum()
} into r2
orderby r2.SupPeopleCount descending
select r2;

查询给我的输出是:

Supervisor: S_1, PeopleCount: 0
Creator: C_3, PeopleCount: 3
Creator: C_2, PeopleCount: 3
Creator: S_1, PeopleCount: 3

这不是我想要的。正确的输出是

Supervisor: S_1, PeopleCount: 3
Creator: C_3, PeopleCount: 3
Creator: C_2, PeopleCount: 3

它以下列方式生成条件聚合器(如果我删除条件中的前缀 $_id 并只留下 $SupId 它就像一个魅力):

"__agg0": {
"$push": {
"$cond": [
{ "$ne": [ "$VolId", "$_id.SupId" ] },
{
"VolId": "$VolId",
"VolFullName": "$VolFullName",
"VolPeopleCount": "$VolPeopleCount"
},
null
]
}
},
"__agg1": {
"$sum": {
"$cond": [
{ "$eq": [ "$VolId", "$_id.SupId" ] },
"$VolPeopleCount",
NumberLong(0)
]
}
}

谢谢!

最佳答案

您可以运行 Select 并在 LINQ 语句中使用三元运算符:

group r1 by new { r1.SupId, r1.SupFullName } into g2
select new
{
g2.Key.SupFullName,
Volunteers = g2.Select(x => x.VolId != x.SupId ? new { x.VolFullName, x.VolPeopleCount }: null)
};

这被转化为以下聚合管道:

{ 
"$group" : {
"_id" : { "SupId" : "$SupId", "SupFullName" : "$SupFullName" },
"__agg0" : {
"$push" : {
"$cond" : [
{ "$ne" : ["$VolId", "$SupId"] },
{ "VolFullName" : "$VolFullName", "VolPeopleCount" : "$VolPeopleCount" },
null
]
}
}
}
},
{ "$project" : { "SupFullName" : "$_id.SupFullName", "Volunteers" : "$__agg0", "_id" : 0 } }

关于c# - 条件聚合器的 MongoDb.Linq 等效查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56346451/

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