codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

16 / 65 topics
12Querying Basics13Find Queries14Projection15Sorting and Limiting16Aggregation Framework17Operators in MongoDB18Text Search
Tutorials/MongoDB/Aggregation Framework
🍃MongoDB

Aggregation Framework

Updated 2026-04-20
3 min read

Introduction to MongoDB Aggregation Framework

The MongoDB Aggregation Framework is a powerful tool for data aggregation, transformation, and analysis. It allows you to process data records and return computed results. This framework is highly flexible and can handle complex data processing tasks efficiently.

Overview of the Aggregation Pipeline

The Aggregation Framework in MongoDB processes documents through a series of stages, each stage transforming the input documents into an output document that is passed on to the next stage. The pipeline consists of one or more stages, where each stage performs a specific operation on the documents.

Basic Stages

  • $match: Filters documents to pass only those that match the specified condition(s) to the next stage.
  • $group: Groups input documents by a specified expression and computes aggregated values for each group.
  • $sort: Sorts all input documents and returns them in sorted order.
  • $project: Reshapes each document in the stream, such as by adding or removing fields, or by renaming or modifying field names and values.
  • $limit: Limits the number of documents passed to the next stage in the pipeline.
  • $skip: Skips over a specified number of documents that pass through the stage.

Detailed Explanation with Code Examples

Example 1: Basic Aggregation Pipeline

Suppose you have a collection named orders with the following documents:

{ "_id" : 1, "item" : "abc", "quantity" : 2, "price" : 10 }
{ "_id" : 2, "item" : "xyz", "quantity" : 1, "price" : 5 }
{ "_id" : 3, "item" : "abc", "quantity" : 4, "price" : 10 }

To calculate the total revenue for each item:

db.orders.aggregate([
   {
     $group: {
        _id: "$item",
        totalRevenue: { $sum: { $multiply: ["$quantity", "$price"] } }
     }
   },
   {
     $sort: { totalRevenue: -1 }
   }
])

Explanation:

  • $group: Groups documents by the item field and calculates the total revenue using $sum with a multiplication of quantity and price.
  • $sort: Sorts the results in descending order based on totalRevenue.

Example 2: Using $match to Filter Documents

To find orders where the quantity is greater than 1:

db.orders.aggregate([
   {
     $match: { quantity: { $gt: 1 } }
   },
   {
     $group: {
        _id: "$item",
        totalQuantity: { $sum: "$quantity" }
     }
   }
])

Explanation:

  • $match: Filters documents where quantity is greater than 1.
  • $group: Groups the filtered documents by item and sums up the quantity.

Example 3: Using $unwind to Deconstruct Arrays

Suppose you have a collection named sales with documents like:

{ "_id" : 1, "product" : "Widget", "sales": [ { "region": "North", "amount": 200 }, { "region": "South", "amount": 300 } ] }
{ "_id" : 2, "product" : "Gadget", "sales": [ { "region": "East", "amount": 400 } ] }

To calculate total sales per region:

db.sales.aggregate([
   {
     $unwind: "$sales"
   },
   {
     $group: {
        _id: "$sales.region",
        totalSales: { $sum: "$sales.amount" }
     }
   }
])

Explanation:

  • $unwind: Deconstructs the sales array field from the input documents to output a document for each element.
  • $group: Groups the deconstructed documents by region and sums up the amount.

Best Practices

  1. Use Indexes: Ensure that fields used in $match, $sort, and $group stages are indexed to improve performance.
  2. Limit Pipeline Stages: Keep the pipeline as short as possible to reduce processing time and memory usage.
  3. Optimize Expressions: Use efficient expressions in aggregation stages to avoid unnecessary computations.
  4. Handle Large Data Sets: Consider using $limit and $skip for pagination, especially with large datasets.

Conclusion

The MongoDB Aggregation Framework is a versatile tool for data analysis and transformation. By understanding the basic stages and best practices, you can effectively use it to process and analyze your data efficiently. Whether you're calculating totals, filtering documents, or reshaping data, the Aggregation Framework provides the necessary tools to meet your requirements.

For more advanced features and detailed documentation, refer to the official MongoDB Aggregation Framework documentation.


PreviousSorting and LimitingNext Operators in MongoDB

Recommended Gear

Sorting and LimitingOperators in MongoDB