Aggregation Commands
Aggregation commands in MongoDB are powerful tools within the aggregation pipeline framework that enable complex data processing and analysis. These commands allow operations such as grouping, sorting and filtering data by making them essential for generating reports, summarizing data and performing advanced data manipulations.
MongoDB Aggregation Commands
Aggregation commands in MongoDB are part of the aggregation pipeline framework. They allow for operations like grouping, sorting, filtering and applying expressions to data. The aggregation pipeline consists of stages each performing a specific operation on the data.
Aggregation commands can be used for tasks such as calculating totals, averages and other statistical operations.
They are useful for generating reports, summarizing data and gaining insights from large datasets. Aggregation commands can also be used to join data from multiple collections or perform complex data manipulations.
Aggregation Commands
Aggregation Command | Description |
---|---|
$match | Filters documents based on specified criteria. |
$group | Groups documents by a specified expression and performs aggregate operations. |
$sort | Sorts documents based on specified fields in ascending or descending order. |
$sum | Calculates the sum of numeric values. |
$avg | Computes the average of numeric values. |
$min | Finds the minimum value in a set of values. |
$max | Finds the maximum value in a set of values. |
$addToSet | Adds unique values to an array. |
$push | Adds values to an array, including duplicates. |
$first | Returns the first document in a group. |
$last | Returns the last document in a group. |
Aggregation Methods
Method | Description |
---|---|
db.collection .aggregate() | Performs aggregation operations on the data in a collection. |
db.collection. mapReduce() | Provides a flexible way to process and aggregate data using JavaScript functions. |
db.collection. distinct() | Returns an array of distinct values for the specified field across a single collection. |
db.collection. countDocuments() | Returns the count of documents that match the specified query criteria. |
db.collection. updateMany() | Updates multiple documents in a collection based on a filter. |
db.collection. find() | Retrieves documents from a collection that match the specified query criteria. |
db.collection. findOne() | Retrieves a single document from a collection that matches the specified query criteria. |
db.collection. deleteMany() | Deletes multiple documents from a collection based on a filter. |
Some Examples of Aggregation Commands
MongoDB provides several aggregation commands to perform different types of operations. Let's understand some of the most commonly used commands:
To understand Common Aggregation Commands we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called products which contains information like name, price, category and quantity of the products in various documents.
[
{
"name": "Product A",
"price": 150,
"category": "Category 1"
},
{
"name": "Product B",
"price": 200,
"category": "Category 2"
},
{
"name": "Product C",
"price": 120,
"category": "Category 1"
},
{
"name": "Product D",
"price": 180,
"category": "Category 2"
}
]
1. $match
The $match command filters documents based on specified criteria, similar to the find() method. It allows users to select only those documents that match the given conditions.
Example:
To retrieve products with a price greater than $100, we can use the $match command:
db.products.aggregate([
{ $match: { price: { $gt: 100 } } }
])
Output:
[
{
"_id": "Category 1",
"total_products": 2
},
{
"_id": "Category 2",
"total_products": 2
}
]
2. $group
The $group command groups documents together based on a specified key and applies aggregate functions to the grouped data, such as sum, count or average.
Example
Continuing with the products collection example, let's group products by their category and calculate the total number of products in each category:
db.products.aggregate([
{ $group: { _id: "$category", total_products: { $sum: 1 } } }
])
Output:
[
{ "_id": "Category 1", "total_products": 2 },
{ "_id": "Category 2", "total_products": 2 }
]
3. $project
The $project command reshapes documents by including, excluding, or renaming fields. It allows users to define the structure of the output documents.
Example:
Suppose we want to retrieve only the name and price fields of products from the products collection. We can use the $project command to include only these fields in the output:
db.products.aggregate([
{ $project: { _id: 0, name: 1, price: 1 } }
])
Output:
[
{ "name": "Product A", "price": 150 },
{ "name": "Product B", "price": 200 },
{ "name": "Product C", "price": 120 },
{ "name": "Product D", "price": 180 }
]
4. $sort
The $sort command sorts documents based on specified fields in ascending or descending order.
Example:
To retrieve products from the products collection sorted by price in descending order, we can use the $sort command:
db.products.aggregate([
{ $sort: { price: -1 } }
])
Output:
[
{
"name": "Product B",
"price": 200,
"category": "Category 2"
},
{
"name": "Product D",
"price": 180,
"category": "Category 2"
},
{
"name": "Product A",
"price": 150,
"category": "Category 1"
},
{
"name": "Product C",
"price": 120,
"category": "Category 1"
}
]
5. $limit
The $limit command restricts the number of documents returned by an aggregation operation.
Example:
If we want to retrieve only the top 5 highest-priced products from the products collection, we can use the $limit command:
db.products.aggregate([
{ $sort: { price: -1 } },
{ $limit: 3 }
])
Output:
[
{ "name": "Product B", "price": 200, "category": "Category 2" },
{ "name": "Product D", "price": 180, "category": "Category 2" },
{ "name": "Product A", "price": 150, "category": "Category 1" }
]
6. Combining Aggregation stages
One of the strengths of MongoDB's aggregation framework is the ability to chain multiple commands together to perform complex operations.
Example:
Suppose we want to find the average price of products in each category from the products collection. We can achieve this by combining the $group and $project commands:
db.products.aggregate([
{ $group: { _id: "$category", avg_price: { $avg: "$price" } } },
{ $project: { _id: 0, category: "$_id", avg_price: 1 } }
])
Output:
[
{
"category": "Category 1",
"avg_price": 135
},
{
"category": "Category 2",
"avg_price": 190
}
]
Explanation: This aggregation pipeline first groups products by category, calculates the average price for each category, and then projects the category and average price fields in the output.
Conclusion
Overall, MongoDB's aggregation framework is a versatile and powerful tool for data analysis and manipulation. It allows users to perform complex operations and derive insights from large datasets. By utilizing various aggregation commands, developers can effectively filter, group, sort, and compute data, making it an essential feature for modern applications.
FAQs
What is the difference between the $match
and $group
aggregation commands?
The
$match
command filters documents based on specified criteria, similar to thefind()
method. The$group
command, on the other hand, groups documents together based on a specified key and performs aggregate functions on the grouped data, such as sum, count, or average.
How does MongoDB's aggregate()
method differ from the mapReduce()
method?
The
aggregate()
method uses a pipeline approach to process and transform data through multiple stages, making it more efficient for many use cases. ThemapReduce()
method, which uses JavaScript functions to map and reduce data, provides more flexibility but can be slower and more resource-intensive for large datasets.
Can aggregation commands in MongoDB join data from multiple collections?
Yes, aggregation commands can join data from multiple collections using the
$lookup
stage, which performs a left outer join to combine documents from different collections based on specified conditions