Aggregation Intro
Pipelines in stages
Aggregation processes documents through stages such as $match, $group, $sort, and $project.
Count by year
This pipeline filters, then groups books by year.
db.books.aggregate([
{ $match: { year: { $gte: 2020 } } },
{ $group: { _id: "$year", total: { $sum: 1 } } },
{ $sort: { _id: 1 } }
])
Match early
Put $match first when possible so later stages see fewer documents. Indexes can support early matches.
Grow skills gradually
Aggregation can reshape arrays, join collections with $lookup, and compute windows. Start with match/group/sort, then expand.
Comments
One comment per signed-in account. Comments are saved with this page’s URL.