MongoDB’s aggregation framework is powerful but intimidating. There are dozens of operators… so which ones actually matter?
This post gives you the 10 aggregation operators you’ll use the most explained in plain English, with tiny examples that answer real-world questions.
Master these, and you can build 90% of the reports, dashboards, and transformations your application needs.
👉 If you’re new to aggregation, start with out MongoDB aggregation pipeline beginners guide
1. $match
Filter documents, like .find() but inside a pipeline.
💡 “Can I only see orders from 2024?”
{ $match: { year: 2024 } }
🔍 Use this early in the pipeline to reduce the amount of data you work with (For better performance).
2. $project
Include, exclude, or reshape fields.
💡 “Can I just return the product name and price?”
{ $project: { name: 1, price: 1, _id: 0 } }Easy! 1 for included fields, 0 for excluded ones.
💡 You can also rename or compute new fields in $project stage.
3. $group
Group documents together and calculate totals, averages, etc.
💡 “What’s the total revenue per category?”
{
$group: {
_id: "$category",
totalRevenue: { $sum: "$price" }
}
}
🧠 SQL friends: GROUP BY
4. $sort
Sort documents by one or more fields.
💡 “Show top-selling products first.”
{ $sort: { sales: -1 } }
Got it? 1 for ascending, -1 for descending.
Use it near the end, unless you’re sorting before a $group (for better performance).
Read also> How to sort without killing performance in MongoDB aggregation pipeline?
5. $limit
Limit the number of results returned.
💡 “Just give me the top 5 users.”
{ $limit: 5 }
Pairs perfectly with $sort.
6. $skip
Skip a number of results, often used for pagination.
💡 “Give me page 2 of the leaderboard (after 10 users).”
{ $skip: 10 }
7. $unwind
Flatten arrays into multiple documents.
💡 “Can I turn each item in an order into a separate row?”
{ $unwind: "$items" }
Essential when working with arrays and complex schemas.
8. $lookup
Join documents from another collection.
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user"
}
}
🧠 SQL friends: JOIN.
9. $addFields
Add or calculate new fields.
💡 “Can I add a totalPrice field (price × quantity) to my output?”
{ $addFields: { totalPrice: { $multiply: ["$price", "$quantity"] } } }
10. $count
Quickly count documents in a pipeline.
💡 “How many orders over $100 do we have?”
{ $match: { total: { $gt: 100 } } },
{ $count: "highValueOrders" }
Sweet and effective.
💡 Bonus: Test Your Pipeline Visually with Mongo Pilot
Instead of writing raw pipelines and reading walls of JSON, Mongo Pilot helps you:
- Build and edit aggregation pipelines visually (drag and drop visual editor)
- Preview each stage’s output to debug
- Understand performance with real query stats
- Try new MongoDB indexes before deploying them
- Easily search across the pipeline content
Perfect if you want to experiment safely and learn fast.
👉 Try it for free
📌 TL;DR, MongoDB Aggregation Operators Cheat Sheet
| Aggregation operator/stage | Purpose |
|---|---|
| $match | Filter documents |
$project | Include/exclude/reshape fields |
$group | Aggregate data |
$sort | Order documents |
$limit | Cap result count |
$skip | Pagination |
$unwind | Flatten arrays |
$lookup | Join collections |
$addFields | Add computed fields |
$count | Count documents |
Now that you understand MongoDB Aggregation pipelines, why not test your knowledge? Take our timed MongoDB quiz and see how you rank against other developers!
