How To Use MongoDB Aggregation Framework in NodeJS?
Last Updated :
22 Jul, 2024
The MongoDB aggregation framework in Node.js performs data aggregation operations such as filtering, grouping and transforming documents within a collection. This is achieved using aggregation pipelines. An aggregation pipeline consists of multiple stages, with an output of using stage being passed to the next stage as input. This allows to processing of the data and returns computed results. An Aggregation pipeline can have one or more stages, each processing using expression operators.
Key Stages in Aggregation Pipeline
The MongoDB aggregation framework provides several stages that can be used to perform various data processing activities. Some of the key stages include:
The `$match` stage filters documents based on a specified condition. It is similar to the find operation.
Syntax:
{ $match: { <field>: <value> } }
Example:
{ $match: { age: { $gte: 18 } } }
This will output only the documents where the age field is greater than or equal to 18.
The $group stage groups documents by a specified identifier and applies accumulator expressions to each group.
Syntax:
{ $group: { _id: <expression>, <field>: { <accumulator>: <expression> } } }
Example:
{ $group: { _id: "$city", totalPopulation: { $sum: "$population" } } }
This will group documents by the city field and calculate the total population for each city.
The `$project` stage reshapes each document by including, excluding, or adding new fields.
Syntax:
{ $project: { <field>: <1 or 0> } }
Example:
{ $project: { name: 1, age: 1, _id: 0 } }
This will include only the name and age fields in the output documents.
The `$sort` stage sorts input documents using a given field in either ascending (1) or descending (-1) order.
Syntax:
{ $sort: { <field>: 1 or -1 } }
Example:
{ $sort: { age: -1 } }
This will sort the documents by the age field in descending order.
The `$limit` stage limits the number of documents passed as output to the next stage.
Syntax:
{ $limit: <number> }
Example:
{ $limit: 5 }
This will output the first 5 documents to the next stage.
The $skip stage skips the first N documents and passes the remaining documents.
Syntax:
{ $skip: <number> }
Example
{ $skip: 10 }
This will skip the first 10 documents and output the rest.
Steps to Implement MongoDB Aggregation Framework
Step 1: Create a node.js project and Install Required Modules
To utilize the MongoDB aggregation framework in a Node.js application, Create a node.js project and install the mongodb package using:
npm init
npm install mongodb
Step 2: Create an index.js File and modify index.js file
Create a file named index.js and add the following code:
JavaScript
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const dbName = 'mydatabase';
async function main() {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('documents');
const pipeline = [
{ $match: { age: { $gte: 18 } } },
{ $group: { _id: "$city", totalPopulation: { $sum: "$population" } } },
{ $sort: { totalPopulation: -1 } },
{ $limit: 5 }
];
const result = await collection.aggregate(pipeline).toArray();
console.log(result);
}
main();
Step 3: Update package.json
Modify the package.json to include all your required dependencies and define the start command.
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"mongodb": "^6.8.0"
}
Step 4: Run the application
Insert some placeholder data into your MongoDB collection and run the application using 'npm start'.
Example data
[
{
"name": "John",
"age": 25,
"city": "New York",
"population": 10000
},
{
"name": "Jane",
"age": 30,
"city": "New York",
"population": 10000
},
{
"name": "Alice",
"age": 22,
"city": "Los Angeles",
"population": 9000
},
{
"name": "Bob",
"age": 19,
"city": "Chicago",
"population": 5000
},
{
"name": "Charlie",
"age": 35,
"city": "San Francisco",
"population": 7000
}
]
Run the application using below command:
npm start
Output:
aggregation pipeline output
Similar Reads
How to drop collection in MongoDb using Node.js ?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This fo
2 min read
How to Post Data in MongoDB Using NodeJS?
In this tutorial, we will go through the process of creating a simple Node.js application that allows us to post data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple
5 min read
How to Perform Aggregation Operations in MongoDB using Node.js?
Aggregation operations in MongoDB allow you to process data records and return computed results. These operations group values from multiple documents, and perform a variety of operations on the grouped data to return a single result. MongoDB's aggregation framework is powerful and flexible, enablin
3 min read
How To Query For Documents In MongoDB Using NodeJS?
MongoDB is the popular NoSQL database that allows for flexible and scalable data storage. NodeJS and JavaScript runtime built on Chrome's V8 JavaScript engine. It is often used with MongoDB to build powerful and efficient applications. In this article, we will guide you on how to query the documents
4 min read
How to create new Collection in MongoDB using Node.js ?
MongoDB the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data. This for
1 min read
How to Get Data from MongoDB using Node.js?
One can create a simple Node.js application that allows us to get data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. Also, we use the EJS for our front end to render the simple HTML form and a table to show the data. Prerequisi
6 min read
How to use $group for a MongoDB query in Node.js ?
The $group stage in MongoDB's aggregation framework is a powerful tool for grouping documents by a specified field and performing various operations on the grouped data, such as calculating totals, averages, and other aggregates. When combined with Node.js, this allows for efficient data processing
3 min read
Aggregation in MongoDB using Python
MongoDB is free, open-source,cross-platform and document-oriented database management system(dbms). It is a NoSQL type of database. It store the data in BSON format on hard disk. BSON is binary form for representing simple data structure, associative array and various data types in MongoDB. NoSQL is
2 min read
How to Retrieve Data from MongoDB Using NodeJS?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. It means that MongoDB isnât based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. Thi
3 min read
How to Create Indexes in MongoDB using Node.js?
MongoDB, a popular NoSQL database, provides powerful indexing capabilities to improve query performance. Indexes in MongoDB help in quickly locating documents and speeding up read operations. In this tutorial, we'll explore how to create indexes in MongoDB using Node.js. What is an Index in MongoDB?
3 min read