Skip to main content

πŸ¦Έβ€β™‚οΈ Repeating stages

info

Extra activity, do it if you have extra time or are following at home, won't be covered during the hands-on Lab

From the Aggregation Pipelines manual.

All stages except the $out, $merge, $geoNear, $changeStream, and $changeStreamSplitLargeEvent stages can appear multiple times in a pipeline.

So we can repeat most stages, and do something like this to get all books from 1985 with more than 100 pages (although it can make sense or not)

[
{
$match: { pages: {$gte: 100} }
},
{
$match: { year: 1985 }
}
]

πŸ’» Add several $limit stages at the end of the above aggregation, limiting to 1 book and see what happens

Answer
[
{$match: {pages: {$gte: 100}}},
{$match: {year: 2011}},
{$limit: 1},
{$limit: 1},
]