Video Transcript (English)
Hello! In this lesson, we’ll explore the MongoDB aggregation framework, which is used to build multi-stage queries. We’ll learn what aggregation is and how it works, specifically in a Node.js application with MongoDB.
When we build queries by using the aggregation framework, think of the query as comprising discrete stages. Each stage provides output documents that act as an input to the next stage. This sequence of data processing stages is referred to as an aggregation pipeline.
If your query can be executed with find() and you do not require additional processing, use find(). Otherwise, use the aggregation framework, which is essentially a superset of find(). The aggregation framework is more flexible than find() and provides powerful primitive operations.
An aggregation pipeline is composed of different components, including stages and expression operators. Let’s examine these components in more detail.
Aggregation pipelines include built-in stages for finding, sorting, grouping, and projecting documents. You can customize the aggregation stages to meet your needs.
For instance, an aggregation pipeline might include a stage where we match all checking accounts, followed by a stage that sorts the account balances in descending order.
This query pipeline makes it easier to debug and maintain the individual stages within the aggregation.
It also makes it easier for MongoDB to rewrite and optimize the query.
An aggregation pipeline can have one or more stages. A stage can use expression operators such as system variables, field paths, expression operators, literals, and expression objects.
We can think of expression operators as being similar to functions that take the field values as their arguments to compute new field values.
Let’s go through an example of an aggregation pipeline that defines two stages and uses an expression operator. The pipeline will find and return documents that match the specified criteria.
Here’s a sample document from the collection that we’re working with.
For this demonstration, we’ll assume that a banking account document has the structure shown here There’s an account_id, account holder name, account type—which is either savings or checking—a balance, and a field for completed transfers.
We want to get all savings accounts documents with a balance greater than 1000. The name of the account holder should be capitalized. And we only want the balance, account_type, and account_holder fields.
This requirement implies two stages in our pipeline: a $match stage and a $project stage.
The $match stage enables us to filter the documents, and the $project stage allows us to specify the fields that we want to see in the result. We’ll need to use an expression operator within the $project stage to capitalize the name and account_type fields.
The aggregation framework provides a wide range of expression operators, including arithmetic, trigonometric, date, and boolean operators. This example uses a comparison operator, $gt, and a and string expression operator, $toUpper.
Once it’s assembled, the aggregation pipeline can be tested from the MongoDB Shell before we implement it in our programming language of choice.
Awesome work. In this video, we explored the MongoDB aggregation framework, which is a language for filtering, sorting, organizing, and analyzing data. We learned how stages can use expression operators to perform computations on field values. We also created a sample aggregation pipeline that uses stages and expression operators.
You can use the concepts covered in this video to build aggregation pipelines in the programming language of your choice.