Lesson 3: MongoDB Aggregation Stages with PHP: Sort and Project / Learn

Video Transcript (English)

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

Let’s begin with an overview of these 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 allows us to specify the fields returned by the aggregation, by including or excluding certain fields. These requested fields can be existing fields from the input documents or newly computed fields.

A project stage should usually be the last stage in a pipeline because it specifies the exact fields to be returned to the client.

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 checking accounts that have a balance greater than 1500 and sort the results in descending order. The query should return only the original balance in US Dollars, the balance in Great British Pounds, 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.

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

Now let’s implement this pipeline.

Let's write our script!

First, we establish the connection with the MongoDB database using the MongoDB Client, and assign the accounts collection to a variable for easy reference.

Then, we define our aggregation pipeline as an array of stages, and save it as 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 the balance to -1 to sort the accounts in descending order.

In the $project stage, we determine the fields that we want by setting them to a value of 1. We also 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 gbp_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.

Nice!

To run a pipeline, we use the aggregate() method, which accepts an array that we stored in our pipeline variable. We'll assign the result of the aggregate operation to a cursor object and use the cursor to iterate through the resulting documents.

Let's run the pipeline.

Awesome! We can see the documents in our aggregation. We can also confirm that they match the filter in our $match stage, and are sorted in descending order. The documents include the four fields that we indicated in the $project stage, which are account_id, account_type, balance, and gbp_balance, and exclude the default _id.

Well done!

In this video, you learned about the $match, $sort, and $project stages and how to implement them in an aggregation pipeline.