Docs Menu
Docs Home
/ / /
Node.js Driver

Aggregation Tutorials

On this page

  • Overview
  • Aggregation Template App
  • Available Tutorials

Aggregation tutorials provide detailed explanations of common aggregation tasks in a step-by-step format. The tutorials are adapted from examples in the Practical MongoDB Aggregations book by Paul Done.

Each tutorial includes the following sections:

  • Introduction, which describes the purpose and common use cases of the aggregation type. This section also describes the example and desired outcome that the tutorial demonstrates.

  • Before You Get Started, which describes the necessary databases, collections, and sample data that you must have before building the aggregation pipeline and performing the aggregation.

  • Tutorial, which describes how to build and run the aggregation pipeline. This section describes each stage of the completed aggregation tutorial, and then explains how to run and interpret the output of the aggregation.

At the end of each aggregation tutorial, you can find a link to a fully runnable Node.js code file that you can run in your environment.


To learn more about performing aggregations, see the Aggregation guide.

Before you begin following an aggregation tutorial, you must set up a new Node.js app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline in each tutorial.


To learn how to install the driver and connect to MongoDB, see the Download and Install and Create a MongoDB Deployment steps of the Quick Start guide.

Once you install the driver, create a file called agg_tutorial.js. Paste the following code in this file to create an app template for the aggregation tutorials:

const { MongoClient } = require("mongodb");
// Replace the placeholder with your connection string.
const uri = "<connection string>";
const client = new MongoClient(uri);
async function run() {
try {
const aggDB = client.db("agg_tutorials_db");
// Get a reference to relevant collections.
// ... const someColl =
// ... const anotherColl =
// Delete any existing documents in collections.
// ... await someColl.deleteMany({});
// Insert sample data into the collection or collections.
// ... const someData = [ ... ];
// ... await someColl.insertMany(someData);
// Create an empty pipeline array.
const pipeline = [];
// Add code to create pipeline stages.
// ... pipeline.push({ ... })
// Run the aggregation.
// ... const aggregationResult = ...
// Print the aggregation results.
for await (const document of aggregationResult) {
} finally {
await client.close();


In the preceding code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.


To learn how to locate your deployment's connection string, see the Create a Connection String step of the Quick Start guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

const uri = "mongodb+srv://mongodb-example:27017";

To run the completed file after you modify the template for a tutorial, run the following command in your shell:

node agg_tutorial.js


In-Use Encryption


Filtered Subset