MongoDB Aggregation Pipeline: A Visual Guide
The aggregation pipeline is one of MongoDB's most powerful features — and one of the most confusing to learn. This guide breaks it down with practical examples and shows how a visual builder can make the process much faster.
What Is the Aggregation Pipeline?
The aggregation pipeline is a sequence of stages that process documents. Each stage transforms the data and passes the result to the next stage. Think of it as a factory assembly line for your data.
A simple example: find active users, group them by country, count each group, and sort by count:
The Most Common Stages
$match — Filter Documents
Works like find() but inside the pipeline. Place it early to reduce the number of documents processed by subsequent stages.
$group — Aggregate Data
Group documents by a field and compute aggregations: $sum, $avg, $min, $max, $push, $addToSet. The _id field defines the grouping key.
$project — Shape Output
Include, exclude, or compute new fields. Useful for trimming large documents down to just the fields you need.
$lookup — Join Collections
Perform a left outer join with another collection. This is MongoDB's equivalent of a SQL JOIN. You specify the foreign collection, the local field, the foreign field, and an alias for the joined data.
$unwind — Flatten Arrays
Deconstruct an array field, creating one document per array element. Often used after $lookup to normalize joined data.
$sort & $limit
Sort results and limit output. Place $sort before $limit for top-N queries.
Building Pipelines Visually
Writing aggregation pipelines as raw JSON gets complex fast. Mongon's visual pipeline builder lets you add stages from a dropdown, edit each one individually, and see the intermediate results at every step.

Key advantages of a visual builder:
- Stage-by-stage preview — click any stage to see the data at that point
- Drag to reorder — rearrange stages without rewriting
- Date macros — use
#last30daysin $match stages instead of hardcoded dates. Learn more in our date queries guide - Export — copy the raw query or export as a code snippet
All 14 Supported Stages
Mongon's pipeline builder supports all 14 aggregation stages:
Tips for Better Pipelines
- Put $match first — filtering early reduces work for all downstream stages.
- Use $project to slim down — remove fields you don't need before heavy stages like $group.
- Index your $match fields — the first $match stage can use indexes, just like find().
- Preview intermediate results — don't build the whole pipeline at once. Check each stage as you add it.