π¦ΈββοΈ Repeating Stages
info
Extra activity! Do it if you have extra time or are following along at home. It 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 may or may not make sense):
- 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},
])