gpt4 book ai didi

mongodb - fork 聚合管道

转载 作者:行者123 更新时间:2023-12-04 14:29:22 25 4
gpt4 key购买 nike

比如我同时有几个类似的聚合操作

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)是一样的,我认为重复重复的阶段是一种浪费。所以我问是否有一种方法可以 fork 管道,以便它可以共享前两个阶段的结果,如下所示,

db.cases.aggregation([
{$match : query},
{$unwind : "factors"},
forks : [
{... operation 1},
{... operation 2}
]
])

最佳答案

这是不久前的事了,但您知道它已经实现了:

https://docs.mongodb.com/manual/reference/operator/aggregation/facet/

{ $facet:
{
<outputField1>: [ <stage1>, <stage2>, ... ],
<outputField2>: [ <stage1>, <stage2>, ... ],
...

}
}

关于mongodb - fork 聚合管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38047527/

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