1. Reshape stage
$project chooses which fields to keep and can add computed fields, controlling the document's shape.
{ $project: { item: 1, revenue: { $multiply: ["$price", "$qty"] } } }Typorithm · easy
Step 1 of 8 · intro card
Aggregation
$project reshapes documents — picking fields and adding computed ones like revenue from price and qty — then $sort orders the results and $limit takes the top N.
$project controls the output shape: include fields with 1, drop them with 0, and synthesize new ones from expressions.
Expression operators such as $multiply, $add, and $concat let a computed field like revenue derive from other fields.
$sort then orders the stream (-1 descending, 1 ascending), and $limit after it yields a clean top-N result.
$project includes (1), excludes (0), and computes fields.
$multiply and friends build computed values.
$sort orders; $limit takes the top N.
db.orders.aggregate([{ $project: { item: 1, revenue: { $multiply: ["$price", "$qty"] } } }, { $sort: { revenue: -1 } }, { $limit: 5 }])Connect each part of the code to the concept before you type.
$project chooses which fields to keep and can add computed fields, controlling the document's shape.
{ $project: { item: 1, revenue: { $multiply: ["$price", "$qty"] } } }Setting a field to 1 includes it; setting it to 0 excludes it from the output.
item: 1$multiply builds a new field from existing ones; expression operators like $add and $subtract work the same way.
revenue: { $multiply: ["$price", "$qty"] }$sort orders documents; -1 is descending and 1 is ascending.
{ $sort: { revenue: -1 } }$limit caps the output to N documents, giving a top-N when it follows $sort.
{ $limit: 5 }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.