Fork聚合管道 [英] Fork the pipeline of aggregation
本文介绍了Fork聚合管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我同时有几个类似的聚合操作,例如
I have several similar aggregation operations at the same time, for example
db.cases.aggregate([
{$match : query},
{$unwind : "factors"},
//operation 1 of the above result
// ...
])
db.cases.aggregate([
{$match : query},
{$unwind : "factors"},
//operation 2 of the above result
// ...
])
聚合的前两个阶段( $match
, $unwind
)是一样的,我认为重复重复的阶段是一种浪费.所以我在问是否有一种方法可以分叉管道,以便它可以共享前两个阶段的结果,如下所示,
The first two stages of aggregation( $match
, $unwind
) are the same, and I think it would be a waste to repeat the duplicate stages. So I am asking if there exists a way to forking the pipeline, so that it can share the result from the first two stages, as follows,
db.cases.aggregation([
{$match : query},
{$unwind : "factors"},
forks : [
{... operation 1},
{... operation 2}
]
])
推荐答案
这是很久以前的事了,但你知道它已经实施了:
This was a while ago but just so you know it was implemented:
https://docs.mongodb.com/manual/reference/运算符/聚合/方面/
{ $facet:
{
<outputField1>: [ <stage1>, <stage2>, ... ],
<outputField2>: [ <stage1>, <stage2>, ... ],
...
}
}
这篇关于Fork聚合管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文