Aggregation Overview

MongoDB’s aggregation framework performs complex transformations and analysis on documents by building data processing pipelines.

Aggregation Pipeline Stages

  1. $match: Filters documents matching criteria
  2. $group: Groups documents based on specified fields
  3. $project: Selects or renames output fields
  4. $sort: Sorts results
  5. $limit: Limits output document count
  6. $skip: Skips documents

Common Operations Explained

$match Filter

db.users.aggregate([
  {
    $match: {
      age: { $gt: 18 },
      status: "active"
    }
  }
])

$group Grouping

db.orders.aggregate([
    { $match: { status: "completed" } },
    {
        $group: {
            _id: "$customer_id",
            total: { $sum: "$amount" },
            avg: { $avg: "$amount" }
        }
    }
])

$project Projection

db.orders.aggregate([
  {
    $project: {
      customer: 1,
      total: { $multiply: ["$price", "$quantity"] },
      _id: 0
    }
  }
])

$sort Sorting

db.employees.aggregate([
  {
    $sort: {
      department: 1,
      salary: -1
    }
  }
])

Performance Optimization Suggestions

  1. Try to use $match at the pipeline front to reduce number of documents processed
  2. Use indexes reasonably to accelerate aggregation queries
  3. Avoid processing large amounts of data in $group stage
  4. Consider using allowDiskOption option for large datasets