子问题......
“$ facet:在同一组输入文档的单个阶段内处理多个聚合管道。每个子管道在输出文档中都有自己的字段,其结果存储为文档数组。”
var res = db.people.aggregate( [
{ $facet: {
facetName1: [ { $match: { group: 1 } }, { $group: {_id: "$age"} }, { $sort: { _id: 1 } } ],
facetName2: [ { $match: { group: 2 } }, { $group: {_id: "$age"} }, { $sort: { _id: 1 } } ],
facetName3: [ { $match: { group: 3 } }, { $group: {_id: "$age"} }, { $sort: { _id: 1 } } ],
facetName4: [ { $match: { group: 4 } }, { $group: {_id: "$age"} }, { $sort: { _id: 1 } } ],
facetName5: [ { $match: { group: 5 } }, { $group: {_id: "$age"} }, { $sort: { _id: 1 } } ] }
}
]
);
{
"_batch": [
{
"facetName1": [ { "_id": 22 }, { "_id": 23 }, { "_id": 26 }, { "_id": 40 } ],
"facetName2": [ { "_id": 19 }, { "_id": 31 }, { "_id": 32 }, { "_id": 36 } ],
"facetName3": [ { "_id": 21 }, { "_id": 24 }, { "_id": 28 } ],
"facetName4": [ { "_id": 20 }, { "_id": 25 }, { "_id": 27 } ],
"facetName5": [ { "_id": 18 }, { "_id": 29 }, { "_id": 37 } ]
}
]
}
我正在尝试获取子管道数据并进一步处理它以创建数组:
22,23,26,40
19,31,32,36
21, 24, 28
20, 25, 27
18, 29, 37
在$ facet之后用$ project尝试了这个,在$ facet尝试操作facetName1,facetName2,facetName3,facetName4,facetName5数据后尝试了这个,但是这些代码尝试不值得发布。< / p>
从管道中删除js我可以:
var arr1 = [[],[],[],[],[]];
for(j = 0; j < 5; j++) {
for(i= 0; i < res._batch[0]["facetName" + (j+1)].length; i++){
arr1[j].push(res._batch[0]["facetName" + (j+1)][i]._id);
}
}
printjson(arr1);
[[22,23,26,40],[19,31,32,36],[21, 24, 28],[20, 25, 27],[18, 29, 37]]
我不确定是否要求使用过多的Mongo查询语言,因为我正在假设SQL Server存储过程的强大功能?
感谢Veeram的回应 - 在Mongo 3.6.1中,这给出了“errmsg”:“字段'年龄'必须是累加器对象”。
回顾原始matrix column combinations within aggregation pipeline,我尝试使用$ facet尝试模拟多个表别名,因为Mongo中不允许使用明显的集合别名。我构建了人物示例,因为真正的应用程序是一个多人棋牌游戏,其中数据是潜在的移动而且非常复杂。人们的例子想要创建工作组从每个组中挑选一个人,其中工作组中的总和年龄必须符合某些标准......也许是一个非常糟糕的例子?但是我认为显示sql代码来做同样的事情可能会非常有用:
select
a.name,
b.name,
c.name,
d.name,
e.name,
(a.age) + (b.age) + (c.age) + (d.age) + (e.age) as tot_len
from
people a
--
cross join
people b
--
cross join
people c
--
cross join
people d
--
cross join
people e
where
a.grp = 1 and
b.grp = 2 and
c.grp = 3 and
d.grp = 4 and
e.grp = 5 and
(a.age + b.age + c.age + d.age + e.age) % 5 = 0
-- (a.age + b.age + c.age + d.age + e.age) > 110 etc
order by a.name, b.name, c.name, d.name, e.name
结果是:
name name name name name tot_len
Alice Arthur Doris Enid James 155
Alice Arthur Freda Enid Jane 140
Alice Arthur Sue Bob Jane 135
Alice Arthur Sue John Jane 130
Alice Mary Doris Bob Jane 130
Alice Mary Doris John Jane 125
Alice Mary Freda Bob Julie 145
Alice Mary Freda John Julie 140
Alice Mary Sue Enid Jane 125
Alice Ted Doris Enid Jane 145
Alice Ted Freda Bob James 150
Alice Ted Freda Enid Julie 160
Alice Ted Freda John James 145
Alice Ted Sue Bob Julie 155
Alice Ted Sue John Julie 150
Alice Verne Doris Enid James 160
Alice Verne Freda Enid Jane 145
Alice Verne Sue Bob Jane 140
Alice Verne Sue John Jane 135
Brian Arthur Doris Enid Jane 130
Brian Arthur Freda Bob James 135
Brian Arthur Freda Enid Julie 145
Brian Arthur Freda John James 130
Brian Arthur Sue Bob Julie 140
Brian Arthur Sue John Julie 135
Brian Mary Doris Bob Julie 135
Brian Mary Doris John Julie 130
Brian Mary Freda Enid James 125
Brian Mary Sue Bob James 120
Brian Mary Sue Enid Julie 130
Brian Mary Sue John James 115
Brian Ted Doris Bob James 140
Brian Ted Doris Enid Julie 150
Brian Ted Doris John James 135
Brian Ted Freda Bob Jane 125
Brian Ted Freda John Jane 120
Brian Ted Sue Enid James 135
Brian Verne Doris Enid Jane 135
Brian Verne Freda Bob James 140
Brian Verne Freda Enid Julie 150
Brian Verne Freda John James 135
Brian Verne Sue Bob Julie 145
Brian Verne Sue John Julie 140
Fred Arthur Doris Bob James 135
Fred Arthur Doris Enid Julie 145
Fred Arthur Doris John James 130
Fred Arthur Freda Bob Jane 120
Fred Arthur Freda John Jane 115
Fred Arthur Sue Enid James 130
Fred Mary Doris Enid James 125
Fred Mary Freda Enid Jane 110
Fred Mary Sue Bob Jane 105
Fred Mary Sue John Jane 100
Fred Ted Doris Bob Jane 125
Fred Ted Doris John Jane 120
Fred Ted Freda Bob Julie 140
Fred Ted Freda John Julie 135
Fred Ted Sue Enid Jane 120
Fred Verne Doris Bob James 140
Fred Verne Doris Enid Julie 150
Fred Verne Doris John James 135
Fred Verne Freda Bob Jane 125
Fred Verne Freda John Jane 120
Fred Verne Sue Enid James 135
Mark Arthur Doris Bob Jane 125
Mark Arthur Doris John Jane 120
Mark Arthur Freda Bob Julie 140
Mark Arthur Freda John Julie 135
Mark Arthur Sue Enid Jane 120
Mark Mary Doris Enid Jane 115
Mark Mary Freda Bob James 120
Mark Mary Freda Enid Julie 130
Mark Mary Freda John James 115
Mark Mary Sue Bob Julie 125
Mark Mary Sue John Julie 120
Mark Ted Doris Bob Julie 145
Mark Ted Doris John Julie 140
Mark Ted Freda Enid James 135
Mark Ted Sue Bob James 130
Mark Ted Sue Enid Julie 140
Mark Ted Sue John James 125
Mark Verne Doris Bob Jane 130
Mark Verne Doris John Jane 125
Mark Verne Freda Bob Julie 145
Mark Verne Freda John Julie 140
Mark Verne Sue Enid Jane 125