Open In App

Introduction to Mongoose Aggregate API

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The aggregate() method in Mongoose is one of the most powerful features of MongoDB, enabling us to perform advanced data manipulations and transformations directly within your database. It provides a rich, pipeline-based approach to querying and manipulating data, which is essential for handling complex queries, aggregations, and analytics. This article will dive deep into the Mongoose Aggregate API, explaining its usage, syntax, and practical examples for beginners and experienced developers alike.

What is the Mongoose Aggregate() Method?

The Mongoose Aggregate() method is used to execute aggregation pipelines on MongoDB collections. Aggregation pipelines are a series of stages that process data in sequence, allowing you to filter, group, sort, and transform your data to meet specific requirements. It is important to note that the aggregate() method is often used in complex scenarios, such as data summarization, filtering based on calculated values, or handling relationships between documents (similar to SQL joins).

Syntax:

Model.aggregate([<pipeline>]);

Parameters:

  • pipeline: It is used to specify the pipeline array of objects.
  • Model: The Mongoose model representing your MongoDB collection.

Why Use Mongoose Aggregate() Over Regular Queries?

The aggregate() method is particularly useful when we need to perform operations that are too complex for simple queries, such as:

  • Data filtering based on calculated or derived fields
  • Grouping of documents by certain fields (similar to SQL GROUP BY)
  • Joining collections using $lookup
  • Sorting and projecting results in more granular ways than what find() can provide

Mongoose Aggregate() Pipeline Stages

An aggregation pipeline consists of multiple stages, where each stage performs a specific operation on the data. Below are some common stages in an aggregation pipeline:

  • $match: Filters documents similar to the find() method.
  • $group: Groups documents together and can be used to calculate aggregates like sums, averages, etc.
  • $sort: Sorts the documents in a specific order.
  • $project: Reshapes the documents, selecting or excluding fields.
  • $lookup: Performs a join operation between collections.
  • $unwind: Deconstructs an array field from the input documents.

Examples of Using Mongoose Aggregate() Method

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

Example 1: Basic Aggregation Pipeline

In this example, we have established a database connection using mongoose and defined a model over studentSchema. In the end, we are using the aggregate() method on the mongoose model and configuring the pipeline. 

Filename: app.js

JavaScript
// Require mongoose module
const mongoose = require("mongoose");

// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"

const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

const studentSchema = new mongoose.Schema({
    name: { type: String },
    age: { type: Number },
    rollNumber: { type: Number },
});

const Student = connectionObject.model('Student', studentSchema);

Student.aggregate([{ $project: { name: 1, rollNumber: 1,
    _id: 0 } }]).exec((error, resultSet) => {
    if (error) {
        console.log(error);
    } else {
        console.log(resultSet);
    }
})

Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
{ name: 'Student1', rollNumber: 9 },
{ name: 'Student3', rollNumber: 178 },
{ name: 'Student4', rollNumber: 152 },
{ name: 'Student2', rollNumber: 176 }
]

Explanation: In this example, we first connect to a MongoDB database called geeksforgeeks. We define a simple schema for students with name and rollNumber fields, then we use the aggregate() method to sort the students by their roll number in ascending order.

Example 2: Using then and catch for Error Handling

In this example, we use the aggregate() method with the then and catch methods to handle asynchronous operations and errors.

Filename: app.js

JavaScript
// Filename: app.js
const mongoose = require('mongoose');

// Define the Schema for Students
const studentSchema = new mongoose.Schema({
  name: String,
  age: Number
});

// Create the Student Model
const Student = mongoose.model('Student', studentSchema);

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/school', { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    // Aggregate to group by age
    return Student.aggregate([
      { $group: { _id: "$age", count: { $sum: 1 } } }
    ]);
  })
  .then(result => {
    console.log(result);
    mongoose.disconnect();
  })
  .catch(err => {
    console.log(err);
    mongoose.disconnect();
  });

Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
{ "_id": 20, "count": 1 },
{ "_id": 19, "count": 1 },
{ "_id": 24, "count": 1 },
{ "_id": 18, "count": 1 }
]

Explanation: In this example, we use the $group stage to group students by their age and count how many students fall under each age group. The result is an array of objects with age as the _id and the number of students (count) for that age.

Best Practices When Using the Aggregate() Method

  1. Optimize Aggregation Pipelines: Use $match as early as possible in the pipeline to filter out unnecessary documents before performing expensive operations like $lookup or $group.
  2. Use Indexes: Ensure that fields used in the $match stage are indexed for better performance.
  3. Limit Pipeline Complexity: Avoid overly complex pipelines, as they can slow down query execution time.
  4. Error Handling: Always implement proper error handling (.catch()) for asynchronous operations to handle unexpected issues.

Conclusion

The Mongoose aggregate() method is an essential tool for performing complex data manipulations and transformations in MongoDB. With the power of aggregation pipelines, you can filter, group, sort, and perform various other operations on your data, all within the database, reducing the need for additional processing on the application side. By understanding and leveraging the stages of aggregation and the structure of the aggregate() method, you can significantly improve the performance and flexibility of your MongoDB queries, making them more efficient and scalable.


Next Article

Similar Reads