How To Use MongoDB Aggregation Framework in NodeJS?
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:
1. $match
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.
2. $group
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.
3. $project
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.
4. $sort
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.
5. $limit
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.
6. $skip
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:
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:
