Open In App

Aggregation in MongoDB

Last Updated : 25 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB Aggregation is a process for performing advanced data transformations and computations on collections. It uses the aggregation pipeline, where documents pass through a series of stages such as filtering, grouping, sorting, reshaping, and calculating to produce summarized or transformed results.

Aggregation Approaches

MongoDB provides multiple approaches for performing aggregation depending on the complexity and type of data analysis you need to perform.

1. Single Purpose Aggregation

Single-purpose aggregation methods are designed for simple analytical queries. It is used when we need simple access to document like counting the number of documents or for finding all distinct values in a document. It simply provides the access to the common aggregation which provides straightforward aggregation functions like:

  • count(): Returns the number of documents in a collection.
  • distinct(): Retrieves unique values for a specified field.
  • estimatedDocumentCount(): Provides an estimated count of documents.

Example: Counting Users in Each City

Let's consider a single-purpose aggregation example where we find the total number of users in each city from the users collection.

Query:

db.users.aggregate([
  { $group: { _id: "$city", totalUsers: { $sum: 1 } } }
])

Output:

[
  { _id: 'Los Angeles', totalUsers: 1 },
  { _id: 'New York', totalUsers: 1 },
  { _id: 'Chicago', totalUsers: 1 }
]

Explanation:

  • Groups documents by the city field.
  • Uses $sum to count users in each city.
  • Returns documents with city (_id) and totalUsers.

2. MongoDB Aggregation Pipeline

The MongoDB aggregation pipeline is a multi-stage process where each stage transforms documents. The output of one stage becomes the input for the next, with each stage filtering, modifying, or computing on documents until the final result is produced.

The basic pipeline stages are defined below:

  • Filters that will operate like queries.
  • The document transformation that modifies the resultant document.
  • Provide pipeline tools for grouping and sorting documents.
  • Aggregation pipeline can also be used in sharded collection.

Let us discuss the aggregation pipeline with the help of an example:

aggregation pipeline example image

Explanation:

  • In the above example of a collection of "train fares". $match stage filters the documents by the value in class field i.e. class: "first-class" in the first stage and passes the document to the second stage.
  • In the Second Stage, the $group stage groups the documents by the id field to calculate the sum of fare for each unique id.

Here, the aggregate() function is used to perform aggregation. It can have three operators stages, expression and accumulator. These operators work together to achieve final desired outcome.

aggregate function operators

Aggregation Pipeline Method

To understand Aggregation Pipeline Method Let's imagine a collection named userswith some documents for our examples.

Query:

{
  "_id": ObjectId("60a3c7e96e06f64fb5ac0700"),
  "name": "Alice",
  "age": 30,
  "email": "[email protected]",
  "city": "New York"
}
{
  "_id": ObjectId("60a3c7e96e06f64fb5ac0701"),
  "name": "Bob",
  "age": 35,
  "email": "[email protected]",
  "city": "Los Angeles"
}
{
  "_id": ObjectId("60a3c7e96e06f64fb5ac0702"),
  "name": "Charlie",
  "age": 25,
  "email": "[email protected]",
  "city": "Chicago"
}

1. $group: It Groups documents by the city field and calculates the average age using the$avgaccumulator.

db.users.aggregate([
  { $group: { _id: "$city", averageAge: { $avg: "$age" } } }
])

Output:

[
  { _id: 'New York', averageAge: 30 },
  { _id: 'Chicago', averageAge: 25 },
  { _id: 'Los Angeles', averageAge: 35 }
]

2. $project: Include or exclude fields from the output documents.

db.users.aggregate([
  { $project: { name: 1, city: 1, _id: 0 } }
])

Output:

[
  { name: 'Alice', city: 'New York' },
  { name: 'Bob', city: 'Los Angeles' },
  { name: 'Charlie', city: 'Chicago' }
] 

3. $match: Filter documents to pass only those that match the specified condition(s).

db.users.aggregate([
  { $match: { age: { $gt: 30 } } }
])

Output:

[
  {
    _id: ObjectId('60a3c7e96e06f64fb5ac0701'),
    name: 'Bob',
    age: 35,
    email: '[email protected]',
    city: 'Los Angeles'
  }
]

3. $sort: It Sorts documents based on field values

db.users.aggregate([
  { $sort: { age: 1 } }
])

Output:

[
  {
    _id: ObjectId('60a3c7e96e06f64fb5ac0702'),
    name: 'Charlie',
    age: 25,
    email: '[email protected]',
    city: 'Chicago'
  },
  {
    _id: ObjectId('60a3c7e96e06f64fb5ac0700'),
    name: 'Alice',
    age: 30,
    email: '[email protected]',
    city: 'New York'
  },
  {
    _id: ObjectId('60a3c7e96e06f64fb5ac0701'),
    name: 'Bob',
    age: 35,
    email: '[email protected]',
    city: 'Los Angeles'
  }
]

5. $limit: Limit the number of documents passed to the next stage.

db.users.aggregate([
  { $limit: 2 }
])

Output:

[
  {
    _id: ObjectId('60a3c7e96e06f64fb5ac0700'),
    name: 'Alice',
    age: 30,
    email: '[email protected]',
    city: 'New York'
  },
  {
    _id: ObjectId('60a3c7e96e06f64fb5ac0701'),
    name: 'Bob',
    age: 35,
    email: '[email protected]',
    city: 'Los Angeles'
  }
]

How to use MongoDB Aggregation

To use MongoDB for aggregating data, follow below steps:

  • Step 1: Connect to MongoDB: Ensure you are connected to your MongoDB instance.
  • Step 2: Choose the Collection: Select the collection you want to perform aggregation on, such as students.
  • Step 3: Define the Aggregation Pipeline: Create an array of stages, like $group to group documents and perform operations (e.g., calculate the average grade).
  • Step 4: Run the Aggregation Query: Use the aggregate method on the collection with your defined pipeline.

Example: Calculating Average Student Grade

This calculates the average grade of all students in the students collection.

Query:

db.students.aggregate([
  {
    $group: {
      _id: null,
      averageGrade: { $avg: "$grade" }
    }
  }
])

Output:

[
  { "_id": null, "averageGrade": 85 }
]

How Fast is MongoDB Aggregation?

  • Aggregation speed depends on pipeline complexity, data size, server hardware, and index efficiency.
  • MongoDB’s aggregation framework is designed to handle large data sets and complex operations efficiently.
  • Proper query optimization and use of indexes improve performance.
  • Server configuration plays a key role in ensuring fast and scalable aggregation.
  • Performance can vary by use case and setup, so monitoring and tuning are important.

Article Tags :

Explore