1. Filter stage
$match keeps only documents that satisfy the condition, like a WHERE clause, and should run early so later stages process fewer docs.
{ $match: { status: "paid" } }Typorithm · easy
Step 1 of 8 · intro card
Aggregation
An aggregation pipeline runs documents through ordered stages. $match filters first, then $group buckets by a key and runs accumulators like $sum and $avg over each bucket.
aggregate() takes an array of stages, and each document flows through them in order, like a Unix pipe.
$match filters documents up front so the heavier stages downstream see fewer of them — it is the cheapest place to cut data.
$group collapses documents that share an _id, and accumulators such as $sum, $avg, and $count compute one value per bucket.
$match filters like a WHERE clause.
$group buckets by its _id key.
$sum / $avg / $count accumulate per bucket.
db.orders.aggregate([{ $match: { status: "paid" } }, { $group: { _id: "$customerId", total: { $sum: "$amount" } } }])Connect each part of the code to the concept before you type.
$match keeps only documents that satisfy the condition, like a WHERE clause, and should run early so later stages process fewer docs.
{ $match: { status: "paid" } }$group buckets documents by _id and runs accumulators over each bucket.
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }The _id of $group is the key to group by; here every order with the same customerId collapses into one result.
_id: "$customerId"$sum adds the field across the group; $avg, $min, $max, and $count work the same way.
total: { $sum: "$amount" }Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.