π¦ΈββοΈ 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)
- Atlas UI
- MongoDB Shell
[
{
$match: { pages: {$gte: 100} }
},
{
$match: { year: 1985 }
}
]
db.books.aggregate([{$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
- Atlas UI
- MongoDB Shell
[
{$match: {pages: {$gte: 100}}},
{$match: {year: 2011}},
{$limit: 1},
{$limit: 1},
]
db.books.aggregate([
{$match: {pages: {$gte: 100}}},
{$match: {year: 2011}},
{$limit: 1},
{$limit: 1},
])