Lesson 2: Using MongoDB Aggregation Stages with Node.js: $match and $group / Learn

Video Transcript (English)

Hello! In this video, you’ll learn how to use the MongoDB Node.js driver to create an aggregation pipeline.

Specifically, you’ll learn how to use the $match and $group stages in an aggregation pipeline.

Let’s start with an overview of the $match and $group stages.

The $match stage filters documents to pass only the documents that match the specified conditions to the next stage of the pipeline.

The $group stage separates documents into groups according to a group key. The output of this stage is one document for each unique group key.

Now that you know the basics, let’s build an aggregation pipeline with the $match and $group stages that: Finds accounts with a balance of less than $1,000, Groups accounts by account type, either checking or savings, and Calculates the average balance for each type.

For this example, we established a connection to MongoDB and initialized the collection object. We’ll use the banking accounts dataset, called `bank`.

Here’s an example of a document from the collection to give you an idea of the account document structure.

Each document includes an account_id, account_holder, account_type, balance, and transfers_complete field. We’ll perform an aggregation on this dataset.

To perform an aggregation, we can pass a list of one or more aggregation stages to the aggregate method. This list can be stored as a JavaScript array that’s called `pipeline`.

In this demonstration, we’ll first go over how the aggregate method works, and then we’ll examine the pipeline stages in the pipeline variable.

We’ll open VS Code and load the sample app.

In this example, we have a function called `main` that encapsulates our logic for establishing a connection and running our pipeline. As usual, we identify the database and the collection we’ll use and assign it to a variable for reference.

To run a pipeline, we use the .aggregate() method, which accepts an array that we stored in our pipeline variable. This method returns a promise, which means that we need to await the result and catch any potential errors. To do this, we use a try/catch block. In the `finally` block, we close the client connection.

Now that we’ve gone over the main function and aggregate method, let’s review the code for the pipeline stages.

The first stage matches accounts that have a balance of less than $1000. To implement this, we add a stage with the $match operator. The $match operator allows only documents that match the provided query conditions to continue to the next stage.

The $lt operator is used in the query condition to state that we want only documents that have a balance of less than 1000 USD.

Next, we need to find the average balance and total balance for all the savings and checking accounts, respectively. To do this, we’ll add the $group stage to our pipeline to summarize this data in the collection.

So go through the code for this example.

In this second stage, we use $group followed by _id. The _id tells the group stage which key-value pair to group by, which in this case is `account_type`. Notice that we use account_type as the key, preceded by a dollar sign and wrapped in quotation marks.

The $group stage also uses operators known as accumulators. These include the $avg and $sum accumulators that help to summarize the data. Here, the balance of account types are added to find the total_balance and averaged to find the average_balance.

Now that we’ve reviewed the pipeline, let’s run it! This is the output from the aggregation pipeline that uses both $match and $group.

We have two individual documents that contain: The _id that identifies the grouping value, checking or savings, The average balance of each type of account, and The total balance of the accounts.

Well done! In this video, you learned about the $match and $group stages and how to implement them in an aggregation pipeline.