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
// ...
])
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/operator/aggregation/facet/
{ $facet:
{
<outputField1>: [ <stage1>, <stage2>, ... ],
<outputField2>: [ <stage1>, <stage2>, ... ],
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With