
MongoDB Aggregation Pipeline Tutorial for Beginners in 2025
- Introduction to MongoDB
- Installation & Database Creation
- CRUD Operations
- Embedded Documents and Arrays
- Validation Rules - Enforcing Structure in MongoDB
- Visualize MongoDB Relationships (Embedded vs Referenced)
- What Is an Index in MongoDB?
- Aggregation Pipeline Explained (You are here).
What Is an Aggregation Pipeline?
An aggregation pipeline is a way to process data step by step. Each step does one specific task, like filtering, grouping, sorting, or transforming documents.
Use it when you need to summarize data, generate reports, or reshape documents, all directly inside MongoDB, without writing extra logic in your application.
You use the .aggregate()
method in MongoDB and pass it a list of these stages.
Common stages you’ll use:
$match
– filters documents (like a WHERE clause)$group
– groups documents and performs calculations$sort
– sorts the result set$project
– selects specific fields$limit
– returns a fixed number of documents$addFields
– adds or modifies fields$lookup
– joins documents from another collection
Common operators used inside stages:
$sum
– adds up values (used in$group
)$avg
– calculates the average (used in$group
)$month
– extracts the month from a date field$toDate
– converts a string into a date
We’ll use several of these in the examples below.
Basic Structure
Here’s the basic structure of an aggregation query:
|
Each stage starts with a $
(like $match
or $group
) and tells MongoDB what to do in that step.
Example 1: Filter and Sort Reviews
Get all reviews for one movie, and sort them by rating (highest first):
|
$match
filters the reviews down to just the ones for that movie.$sort
orders them by rating, descending.
This is the output after running the $match
and $sort
query in the DbSchema Tool.
Example 2: Average Rating per Movie
Group the reviews by movie and calculate the average rating:
|
$group
groups reviews by movie_id.$avg
calculates the average of therating
field.
Here’s what the result looks like inside the $match
and $sort
query in the DbSchema Tool.
Example 3: Reviews per Month
Count how many reviews were posted each month:
|
$month
extracts the month number from thereview_date
.$sum: 1
adds one for each document (counts them).$sort
orders the results by month.
This is the output after running the $month
and $sum
query in the DbSchema Tool.
Example 4: Show Only Specific Fields
Return just the movie title and year, and hide the description:
|
1
includes a field.0
excludes a field.
Using the query editor for $project
query in the DbSchema Tool.
Summary
Aggregation pipelines are a clean way to filter, group, and reshape data directly inside MongoDB.
They’re useful for things like:
- Generating reports
- Building analytics
- Creating summaries
- Reducing logic in your app
In the next chapter, you’ll learn how to use $lookup
to combine data from different collections - similar to a join in SQL.