DbSchema Database Designer

MongoDB Aggregation Pipeline Tutorial for Beginners in 2025



Visualize MongoDB Relationships

  1. Introduction to MongoDB
  2. Installation & Database Creation
  3. CRUD Operations
  4. Embedded Documents and Arrays
  5. Validation Rules - Enforcing Structure in MongoDB
  6. Visualize MongoDB Relationships (Embedded vs Referenced)
  7. What Is an Index in MongoDB?
  8. 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 } }
])

  • $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.

Aggregation Pipelines $match $sort functions

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" }
}
}
])

  • $group groups reviews by movie_id.
  • $avg calculates the average of the rating field.

Here’s what the result looks like inside the $match and $sort query in the DbSchema Tool.

Aggregation Pipelines $group $avg functions

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 } }
])

  • $month extracts the month number from the review_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.

Aggregation Pipelines $sum function

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
}
}
])

  • 1 includes a field.

  • 0 excludes a field.

Using the query editor for $project query in the DbSchema Tool.

Aggregation Pipelines $project function

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.

DbSchema Database Designer
Visual Design & Schema Diagram

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.