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:
db.collection.aggregate([
{ stage1 },
{ stage2 },
...
])
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):
db.reviews.aggregate([
{ $match: { movie_id: ObjectId("000000000000000000001000") } },
{ $sort: { rating: -1 } }
])
-
$matchfilters the reviews down to just the ones for that movie. -
$sortorders 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:
db.reviews.aggregate([
{
$group: {
_id: "$movie_id",
averageRating: { $avg: "$rating" }
}
}
])
$groupgroups reviews by movie_id.$avgcalculates the average of theratingfield.
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:
db.reviews.aggregate([
{
$group: {
_id: { $month: "$review_date" },
totalReviews: { $sum: 1 }
}
},
{ $sort: { _id: 1 } }
])
-
$monthextracts the month number from thereview_date. -
$sum: 1adds one for each document (counts them). -
$sortorders 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:
db.movie.aggregate([
{
$project: {
title: 1,
release_year: 1,
rating: 1
}
}
])
-
1includes a field. -
0excludes 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.