Lesson 3: Using MongoDB Aggregation Stages with Node.js: $sort and $project / Learn

Press O for more options
3:23

Video Transcript (English)

Hello! In this video, you’ll learn how to build an aggregation pipeline composed of the $match, $sort, and $project stages.

Let’s begin with an overview of the $match, $project, and $sort stages.

The $match stage filters documents so that only documents that match the specified conditions are passed to the next stage in the pipeline.

The $sort stage sorts all input documents and returns them to the pipeline in sorted order. To sort in ascending order, we use the value of 1. For descending order, we use the value of -1.

$project passes documents with the requested fields to the next stage of the pipeline. These requested fields can be existing fields from the input documents or newly computed fields.

Now that we’ve covered the stages we’ll use, let’s build an aggregation pipeline that uses $match, $sort, and $project to find all accounts with a balance greater than 1500 and sort the results in descending order. The query should only return the balance, the balance in Great British Pounds (GBP), and the account_type and account_id fields. To compute the GBP balance, we assume an exchange rate of 1 British pound to 1.3 US Dollars.

For this example, we’ll assume that we’ve already established a connection to MongoDB and initialized the collection object. We’ll use the banking accounts dataset.

Here’s an example of a document from the collection to give you an idea of the account document structure. It includes fields for the account_id, account_holder, account_type, balance, and transfers_complete.

Now let’s implement the pipeline by using the $match, $sort and $project stages.

In this example, the pipeline is defined in an array and assigned to a variable named pipeline.

The $match stage filters incoming documents by the account_type of checking with a balance greater than 1500.

Next, in the $sort stage, we set balance to -1 to order the accounts in descending order.

Finally, in $project stage, we determine the fields we want by setting them to a value of 1. We set account_id, account_type, and balance to 1. The _id field is always returned by default, so to omit it, we’ll use a 0.

Finally, we create a new field in the $project stage called gdp_balance. This field calculates the conversion from US Dollars to British pounds. Here, we divide the balance value by 1.3 to generate the conversion value.

Now that all the pipeline stages are defined, we can use them in our Node.js application. To do so, we’ll call the aggregate method on the collection, and then pass in the pipeline variable as an argument.

The aggregate method returns a cursor, which is then iterated though so that each result is logged to the terminal.

Each returned document contains the requested fields, which are account_id, account_type, balance, and a computed gbp_balance.

Well done! In this video, you learned about the match, $sort, and $project stages and how to implement them in an aggregation pipeline. The MongoDB Node.js driver provides us with a wide range of utility classes to simplify aggregations.