Common 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" }
]
Aggregation Commands
MongoDB’s aggregation commands are a powerful feature of its database it allowing users to perform complex operations on their data. These commands are part of the aggregation pipeline framework which consists of stages that can perform operations like grouping, sorting, filtering and applying expressions to data. By using these commands developers can generate reports, summarize data, and gain insights from large datasets.
In this article, We will learn about Aggregation Commands and their different types along with the help of examples and so on.