MongoDB · January 2, 2024

MongoDB Glossary - Aggregation

MongoDB Glossary - Aggregation

In the world of databases, MongoDB has gained significant popularity due to its flexibility and scalability. One of the key features that sets MongoDB apart is its powerful aggregation framework. In this article, we will explore the concept of aggregation in MongoDB and how it can be used to analyze and manipulate data.

What is Aggregation?

Aggregation in MongoDB refers to the process of transforming and manipulating data from multiple documents into a single result. It allows you to perform complex operations on your data, such as grouping, filtering, sorting, and calculating aggregate values.

The aggregation framework in MongoDB provides a set of operators that can be used to build pipelines. A pipeline is a sequence of stages, where each stage performs a specific operation on the data. The output of one stage becomes the input for the next stage, allowing you to chain multiple operations together.

Aggregation Stages

Let's take a look at some of the commonly used stages in the aggregation framework:

  • $match: Filters the documents based on specified criteria.
  • $group: Groups the documents by a specified key and performs aggregate calculations on each group.
  • $sort: Sorts the documents based on specified fields.
  • $project: Reshapes the documents by including or excluding fields.
  • $limit: Limits the number of documents in the output.
  • $skip: Skips a specified number of documents in the input.

These stages can be combined in various ways to achieve the desired result. For example, you can use the $match stage to filter the documents, followed by the $group stage to group them based on a specific field, and then use the $sort stage to sort the groups.

Aggregation Operators

In addition to the stages, MongoDB provides a wide range of aggregation operators that can be used within the stages to perform specific calculations or transformations. Some of the commonly used operators include:

  • $sum: Calculates the sum of numeric values.
  • $avg: Calculates the average of numeric values.
  • $min: Finds the minimum value.
  • $max: Finds the maximum value.
  • $push: Appends a value to an array.
  • $addToSet: Adds a value to a set (array with unique values).

These operators can be used within the $group stage to perform calculations on grouped data or within the $project stage to reshape the documents.

Example

Let's consider a scenario where we have a collection of sales orders. Each document in the collection represents a single order and contains fields like orderDate, product, quantity, and price. We want to calculate the total sales for each product.

Using the aggregation framework, we can achieve this by grouping the documents by the product field and calculating the sum of the quantity multiplied by the price for each group. The resulting output will provide the total sales for each product.

db.sales.aggregate([
  { $group: {
    _id: "$product",
    totalSales: { $sum: { $multiply: ["$quantity", "$price"] } }
  } }
])

This query will return a result like:

[
  { _id: "Product A", totalSales: 5000 },
  { _id: "Product B", totalSales: 8000 },
  { _id: "Product C", totalSales: 3000 }
]

Conclusion

The aggregation framework in MongoDB is a powerful tool for analyzing and manipulating data. It allows you to perform complex operations on your data and obtain meaningful insights. By understanding the various stages and operators available, you can leverage the full potential of MongoDB's aggregation framework to meet your data analysis needs.

If you are interested in exploring more about MongoDB and its features, consider checking out Server.HK, a leading VPS hosting company that provides reliable and scalable hosting solutions.