

Transform and Analyze Data with MongoDB Aggregation
The MongoDB Aggregation Framework is a powerful tool for performing complex data analysis directly within MongoDB. It allows for multi-stage data transformation through an aggregation pipeline, making it ideal for scenarios where you need to process and analyse large datasets.
This differs from simple queries that fetch data directly through the find() command. While find() is useful for fetching documents that match a specific query, the aggregation framework enables more advanced operations like grouping, sorting, filtering, reshaping, and even computing new values.
Why Use Aggregation in MongoDB?
The main reasons to use aggregation inside MongoDB include:
- Efficiency: It allows you to perform analytics without exporting data to another platform, reducing the overhead and cost of data transfer.
- Real-Time Data: Aggregation operates on live data rather than older copies from batch processing systems.
- Powerful Data Manipulation: You can perform operations such as grouping, sorting, and transforming data more efficiently than with simple queries.
- Built-in Functions: MongoDB’s aggregation framework provides many operators and stages to perform tasks that would otherwise require complex code.

Key Concepts in Aggregation
- Pipeline: Aggregation in MongoDB is a multi-stage process where data flows through a series of stages. Each stage performs a specific operation on the data. The output of one stage becomes the input for the next stage.
- Stages: There are many stages available for use in aggregation, including:
- $match: Filters documents to pass only those that match the specified conditions.
- $group: Aggregates documents based on certain criteria, e.g., summing, averaging, or counting.
- $sort: Sorts documents by a specified field.
- $project: Reshapes documents, adding or removing fields.
- $limit: Limits the number of documents in the result set.
- $skip: Skips over a specified number of documents.
- $unwind: Deconstructs an array field from the input documents, creating one document for each element in the array.
- $out: Writes the aggregation result to a new or existing collection.
There are many stages available for use in aggregation, and we’ve shared the most commonly used ones. For more information on additional stages, please refer to the official MongoDB Aggregation Stages page.
Optimising Aggregation Queries with Indexing and Tips
Proper indexing on these stages can significantly improve query performance.
- $match: Index fields you filter by to speed up document selection.
- $group: Index fields used in prior $match stages to reduce the document count.
- $sort: Index fields used for sorting to avoid full dataset scans.
Performance Boost with Indexes and Filters
- Indexes: Index fields in $match, $sort, and $group stages for better performance.
- Document Filters: Use $match, $limit, and $skip early to limit the data processed.
Additional Stage Considerations
- $geoNear: Requires a geospatial index.
- $lookup and $graphLookup: Benefit from indexes on related collections.
.png)
Aggregation Pipeline Example
You have a collection named restaurant_orders, and you want to find the top three dishes ordered in Portland.
db.restaurant_orders.aggregate([
{ $match: {
"city": "Portland"
}},
{
$group: {
_id: "$dish_name",
count: { $sum: 1 }
}},
{
$sort: { count: -1 }},
{
$limit: 3
}])
Explanation:
- $match: Filters the documents to only include orders from Portland.
- $group: Groups the remaining documents by dish_name and counts how many times each dish was ordered.
- $sort: Sorts the result by the count of orders in descending order, so the most popular dishes come first.
- $limit: Limits the result to the top three most ordered dishes.
Result:
The result will show the top three dishes ordered in Portland.
MongoDB Aggregation Syntax
The basic syntax for an aggregation query in MongoDB is:
db.collection.aggregate(pipeline, options)
- collection: The collection you want to run the aggregation on.
- pipeline: An array of stages to be executed in sequence.
- options: Optional settings, such as enabling disk usage when memory limits are exceeded.
Aggregation Operators
Several operators are available for use within aggregation stages. Some key operators include:
- $sum: Sums values across a group of documents.
- $avg: Computes the average of a numeric field.
- $max and $min: Finds the maximum and minimum values of a field.
- $push: Creates an array by accumulating field values.
- $count: Counts the number of documents in a group.
.png)
Handling Large Aggregations
By default, each stage in an aggregation pipeline can use up to 100 MB of RAM. If the memory limit is exceeded, MongoDB will throw an error. To allow MongoDB to use disk for processing, you can set the allowDiskUse option to true:
db.restaurant_orders.aggregate(pipeline, { allowDiskUse: true })
Starting from MongoDB 6.0, operations that need more than 100 MB of memory will automatically use disk for temporary files by default.
You can use allowDiskUse() to control whether MongoDB writes temporary files to disk when memory limits are exceeded.
Practical Example: $project and $group
- $project: To include only certain fields in the result (like city, dish_name, and order_time):
db.restaurant_orders.aggregate([
{ $project: { _id: 0, city: 1, dish_name: 1, order_time: 1 } }])
- $group: To group data and count the number of orders per dish:
db.restaurant_orders.aggregate([
{ $group: { _id: "$dish_name", total_orders: { $sum: 1 } } }])
Conclusion
MongoDB’s aggregation framework enables efficient, complex operations directly on the database, helping with advanced analytics and data processing. The flexibility of the aggregation stages and operators allows you to easily manipulate and analyse data in real-time.
For a more detailed understanding of Aggregation in MongoDB, we’ve shared several blogs that cover different aspects of the topic. Refer to the following:
MongoDB Aggregation Framework: An Overview
Advanced Data Analysis using MongoDB Custom Aggregation Expressions
These resources will provide you with deeper insights into MongoDB’s aggregation capabilities and advanced data analysis techniques.
Looking to implement or optimize MongoDB aggregation in your applications? Explore our Managed MongoDB Services — our team of certified experts can help you design, scale, and maintain robust MongoDB architectures tailored to your data needs.
{{cta}}