Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fork the pipeline of aggregation

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}
   ]
])
like image 898
ppn029012 Avatar asked Dec 18 '25 05:12

ppn029012


1 Answers

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>, ... ],
      ...

   }
}
like image 87
D. Cantatore Avatar answered Dec 20 '25 22:12

D. Cantatore