Introduction to Aggregation

Aggregation is a powerful feature in MongoDB used for processing and analyzing data. It allows you to perform operations on documents, such as filtering, grouping, and sorting, to produce aggregated results. The Aggregation Framework provides a way to transform data in complex ways using a pipeline of stages.

Basic Aggregation Operations

$match

  • Description: Filters documents to pass only those that match the specified criteria.

  • Example:

      db.collectionName.aggregate([
      { $match: { status: "active" } }
    ]);
      

$group

  • Description: Groups documents by a specified field and performs aggregation operations on the grouped documents.

  • Example:

      db.collectionName.aggregate([
      { $group: { _id: "$category", totalAmount: { $sum: "$amount" } } }
    ]);
      

$sort

  • Description: Sorts documents based on specified fields.

  • Example:

      db.collectionName.aggregate([
      { $sort: { date: -1 } } // Sort by date in descending order
    ]);
      

$project

  • Description: Reshapes documents by including, excluding, or adding fields.

  • Example:

      db.collectionName.aggregate([
      { $project: { name: 1, totalAmount: { $multiply: ["$amount", 1.1] } } }
    ]);
      

Pipeline Stages and Operators

Pipeline Stages

  • Description: Stages represent a step in the aggregation pipeline and process documents sequentially.
  • Example Stages:
    • $match: Filter documents.
    • $group: Aggregate and group documents.
    • $sort: Order documents.
    • $project: Shape documents.

Operators

  • Description: Operators perform various operations on fields within documents.
  • Examples:
    • $sum: Calculates the sum of numeric values.
    • $avg: Calculates the average value.
    • $min: Finds the minimum value.
    • $max: Finds the maximum value.

Examples of Aggregation Pipelines

Example 1: Filtering and Grouping

  • Objective: Find the total sales amount for each product category.

  • Pipeline:

      db.sales.aggregate([
      { $match: { status: "completed" } },
      { $group: { _id: "$category", totalSales: { $sum: "$amount" } } },
      { $sort: { totalSales: -1 } }
    ]);
      

Example 2: Reshaping Documents

  • Objective: Project specific fields and add a new field for discounted prices.

  • Pipeline:

      db.products.aggregate([
      { $project: { name: 1, price: 1, discountedPrice: { $multiply: ["$price", 0.9] } } }
    ]);
      

Example 3: Complex Aggregation

  • Objective: Get the average price of products by category and sort by average price.

  • Pipeline:

      db.products.aggregate([
      { $group: { _id: "$category", averagePrice: { $avg: "$price" } } },
      { $sort: { averagePrice: -1 } }
    ]);
      

Summary

  • Aggregation Framework: Provides a way to process and analyze data using a pipeline of stages.
  • Basic Operations: $match, $group, $sort, $project.
  • Pipeline Stages and Operators: Include various stages and operators to transform and analyze data.
  • Examples: Includes common use cases for filtering, grouping, and reshaping documents.