0% completed
Aggregation in MongoDB is a powerful way to process data and return computed results. This framework is designed for efficient data processing and allows for complex transformations and analyses within the database. Aggregation operations group values from multiple documents together and can perform a variety of operations on the grouped data to return a single result.
The MongoDB aggregation framework provides a means to calculate aggregated values without having to use Map-Reduce. The framework is based on the concept of a pipeline, where documents are passed through a sequence of stages, each of which performs a specific operation.
The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that can transform them and output the aggregated results. Each stage in the pipeline performs an operation on the input documents and passes the result to the next stage.
Stage | Description |
---|---|
$match | Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage. |
$group | Groups input documents by the specified _id expression and for each distinct grouping, outputs a document. |
$project | Passes along the documents with only the specified fields to the next stage. |
To understand the aggregation pipeline, consider the following example where we want to group user data by their age and count the number of users in each age group.
First, let's insert some documents into the users
collection to work with:
db.users.insertMany([ { name: "Alice", age: 25, city: "New York" }, { name: "Bob", age: 30, city: "San Francisco" }, { name: "Charlie", age: 35, city: "Los Angeles" }, { name: "David", age: 30, city: "New York" }, { name: "Eve", age: 25, city: "San Francisco" } ])
db.users.aggregate([ { $match: { age: { $gte: 25 } } }, { $group: { _id: "$age", count: { $sum: 1 } } }, { $sort: { _id: 1 } } ])
Explanation:
age
is greater than or equal to 25.age
field and counts the number of documents in each group._id
field in ascending order.The $match
stage filters documents based on a specified condition. This stage limits the number of documents passed to the next stage of the pipeline.
Syntax
{ $match: { <field>: <value> } }
Example
db.users.aggregate([ { $match: { city: "New York" } } ])
This command filters the documents to include only those where the city
is "New York".
The $group
stage groups documents by a specified expression and outputs one document per group. The output documents can contain computed fields that hold the result of an aggregation operation on the documents in each group.
Syntax
{ $group: { _id: <expression>, <field>: { <accumulator>: <expression> } } }
Example
db.users.aggregate([ { $group: { _id: "$city", totalAge: { $sum: "$age" } } } ])
This command groups documents by the city
field and calculates the total age of users in each city.
The $project
stage reshapes each document in the stream, such as by adding new fields or removing existing fields.
Syntax
{ $project: { <field1>: <include|exclude>, <field2>: <include|exclude>, ... } }
Example
db.users.aggregate([ { $project: { name: 1, age: 1, _id: 0 } } ])
This command includes only the name
and age
fields in the output documents and excludes the _id
field.
The MongoDB aggregation framework is a powerful tool for data analysis and transformation. By understanding and utilizing the various stages and operators available in the aggregation pipeline, you can perform complex queries and aggregations efficiently within your MongoDB database. This lesson provides an introduction to the key concepts and basic usage of the aggregation pipeline, laying the foundation for more advanced topics in subsequent lessons.
.....
.....
.....