Lecture Notes Of Day 15:
MongoDB Aggregation Operators
Objective:
- Understand
how to use aggregation operators like $group, $sum, $avg, and $count in
MongoDB.
- Learn
how these operators are applied within the aggregation pipeline to create
complex aggregation queries.
Outcome:
- Students
will be able to create complex aggregation queries using operators such as
$group, $sum, $avg, and $count to manipulate and summarize data in
MongoDB.
1.
Introduction to MongoDB Aggregation
MongoDB provides a powerful
framework called the Aggregation Framework, which is used to process and
transform data. It allows you to perform operations on data, such as filtering,
grouping, sorting, and calculating values in a flexible and efficient manner.
Aggregation operates on the
concept of stages, where each stage performs a specific operation on the
input data and passes the result to the next stage.
2.
Aggregation Pipeline Stages
- $match:
Filters documents based on specified criteria (like SQL WHERE clause).
- $group:
Groups documents together for aggregation purposes (like SQL GROUP BY
clause).
- $sum, $avg,
$count: Operators used within the $group stage to perform
calculations.
The aggregation pipeline consists
of multiple stages, and each stage is separated by a comma. For example:
javascript
CopyEdit
db.collection.aggregate([
{ $match: { age: { $gt: 25 } } }, // Filters documents with age > 25
{ $group: { _id: "$department", totalSalary:
{ $sum: "$salary" } } } // Groups by department and sums the salary
]);
3.
Aggregation Operators Explained
3.1 $group
Operator
The $group operator is used to
group documents based on a specified field and perform calculations on those
groups. Each group must have an _id field, which represents the field you are
grouping by.
Example:
javascript
CopyEdit
db.orders.aggregate([
{ $group: { _id: "$category", totalRevenue:
{ $sum: { $multiply: ["$price", "$quantity"] } } } }
]);
- _id:
Specifies the field to group by (e.g., category).
- totalRevenue:
Represents the aggregated field, in this case, the sum of price * quantity.
3.2 $sum
Operator
The $sum operator is used within
the $group stage to calculate the sum of numeric values in a group.
Example:
javascript
CopyEdit
db.orders.aggregate([
{ $group: { _id: null, totalAmount: { $sum: "$amount"
} } }
]);
- This
calculates the total amount of all orders in the collection.
3.3 $avg
Operator
The $avg operator calculates the
average of the numeric values for a given field.
Example:
javascript
CopyEdit
db.employees.aggregate([
{ $group: { _id: "$department", avgSalary:
{ $avg: "$salary" } } }
]);
- This
example groups employees by department and calculates the average salary
for each department.
3.4 $count
Operator
The $count operator counts the
number of documents in the pipeline.
Example:
javascript
CopyEdit
db.orders.aggregate([
{ $match: { status: "delivered" }
},
{ $count: "totalDeliveredOrders" }
]);
- This
counts how many orders have the status "delivered" and outputs the
result as a field named totalDeliveredOrders.
4.
Combining Aggregation Operators
You can use multiple aggregation
operators in a single pipeline to perform more complex calculations. Here's an
example that uses $match, $group, $sum, and $avg operators:
javascript
CopyEdit
db.sales.aggregate([
{ $match: { year: 2023 } },
{ $group: {
_id: "$region",
totalSales: { $sum: "$amount"
},
avgSales: { $avg: "$amount" }
}
}
]);
- This
query filters sales for the year 2023 and then groups the results by
region, calculating both the total and average sales for each region.
5.
Practical Exercises
Exercise
1: Sum of Sales
Task:
Create a collection sales with fields: productId, quantity, and price. Write an
aggregation query to calculate the total revenue by summing the product of quantity
and price.
Solution:
javascript
CopyEdit
db.sales.aggregate([
{ $group: {
_id: null,
totalRevenue: { $sum: { $multiply: ["$quantity",
"$price"] } }
}
}
]);
Exercise
2: Average Salary by Department
Task:
Create a collection employees with fields: name, department, and salary. Write
an aggregation query to calculate the average salary per department.
Solution:
javascript
CopyEdit
db.employees.aggregate([
{ $group: {
_id: "$department",
avgSalary: { $avg: "$salary" }
}
}
]);
Exercise
3: Count of Orders by Status
Task:
Create a collection orders with fields: orderId, status, and amount. Write an
aggregation query to count how many orders have the status "completed".
Solution:
javascript
CopyEdit
db.orders.aggregate([
{ $match: { status: "completed" }
},
{ $count: "completedOrders" }
]);
6.
Conclusion
- MongoDB’s
aggregation operators, like $group, $sum, $avg, and $count, allow you to
perform powerful data manipulations.
- Understanding
these operators is crucial for writing complex queries that can transform
raw data into meaningful summaries.
- The
aggregation framework is highly flexible and can be combined with various
stages and operators to achieve a wide range of data analysis tasks.
Additional
Reading
By the end of this lesson,
students should have a solid understanding of how to leverage MongoDB’s
aggregation operators to create powerful queries for summarizing and analyzing
data.
