Top 10 MongoDB Aggregation Operators You Should Master

The top 10 MongoDB aggregation pipeline operators with simple examples

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/stagePurpose
$matchFilter documents
$projectInclude/exclude/reshape fields
$groupAggregate data
$sortOrder documents
$limitCap result count
$skipPagination
$unwindFlatten arrays
$lookupJoin collections
$addFieldsAdd computed fields
$countCount 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!

AymenLoukil
Founder, Technical Entrepreneur, International Consultant, Public Speaker

More from the Mongo Pilot Blog

Why You Need a GUI for MongoDB

Why You Need a GUI for MongoDB

MongoDB is powerful, flexible, and widely adopted but let’s be honest: working only from the shell can be… painful. You can query, aggregate, and manage your data with the CLI, but as soon as your collections grow, your productivity drops. That’s where a MongoDB GUI (Graphical User Interface) comes in. Let’s break down why a

How to Sort in MongoDB Aggregation Without Killing Performance

How to Sort in MongoDB Aggregation Without Killing Performance

Sorting in MongoDB aggregations seems straightforward until your query slows your server to a crawl. Why? The $sort stage can be a performance bottleneck, especially on large datasets. This post explains why and shares proven strategies to optimize sorting, keeping your queries fast and efficient. (Based on MongoDB 8.0 documentation.) Why $sort can Hurt Performance?

What is an Index in MongoDB (and Why It Matters)

What is an Index in MongoDB (and Why It Matters)

When your MongoDB queries slow down, it’s often because you’re missing the right index. And it’s one of the common MongoDB query mistakes. An index in MongoDB is like the index of a book: it helps you jump directly to the data you need instead of flipping through every page. Without indexes, MongoDB performs a

Leave a Comment

The smartest MongoDB GUI

MongoPilot empowers developers to manage MongoDB databases effortlessly with a local LLM for AI-driven queries and an intuitive visual query builder.

Mongo Pilot, smart MongoDB GUI

MongoDB GUI

Smart MongoDB GUI with AI capabilities