Video Transcript (English)
Hello! In this video, we’ll explore the MongoDB aggregation framework, which is used to build multi-stage queries. You’ll learn what aggregation is and how it works.
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 does not require additional processing, use find(). If you need to process documents and return computed results, use the aggregation framework. For example, you would want to use aggregation when you need to summarize data or create new calculated fields.
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 fit your needs.
For instance, an aggregation pipeline might include a stage where we match all bank accounts opened in a specific year, followed by a stage that groups accounts by the month in which they were opened, and then calculates the total balance of all accounts opened that month.
Structuring aggregations as pipelines makes it easier to debug and maintain the individual stages within the aggregation.
It also makes it easier for MongoDB to optimize the query.
An aggregation pipeline can have one or more stages. A stage can use expression operators such as system variables, field paths, literals, and expression objects.
We can think of expression operators like functions that take arguments, such as field values, and evaluate to a result.
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 select all savings accounts documents that have a balance greater than 1000. The names of the account holder and account_type should be capitalized. And we want to return only the balance, account_type, and account_holder fields.
This requirement necessitates 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 in the result.
We need to use an expression operator within the $project stage to capitalize the name and account_type fields. The _id field is always returned by default, so to omit it, we’ll use a 0. By the way, projections are an option with find() and findOne() and are not specific to the aggregation framework. However, find() and findOne() don’t have access to the full arsenal of expressions supported by the aggregation pipeline’s $project stage.
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 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. You learned how stages can use expression operators to perform computations on field values. Then we 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.