#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Stages, Operators, and Building Complex Data Transformations

Aggregation Pipeline

The aggregation pipeline is MongoDB's framework for transforming and analyzing data across multiple stages — similar to a Unix pipe, where each stage's output feeds into the next.

1. Pipeline Basics

db.orders.aggregate([
  { $match: { status: "completed" } },
  { $group: { _id: "$customerId", totalSpent: { $sum: "$amount" } } },
  { $sort: { totalSpent: -1 } }
])

2. Common Pipeline Stages

StagePurpose
$matchFilter documents (like find())
$groupGroup documents and compute aggregates
$projectReshape documents — include/exclude/compute fields
$sortOrder documents
$limit / $skipPaginate results within the pipeline
$countCount matching documents

3. Grouping and Accumulator Operators

db.sales.aggregate([
  { $group: {
      _id: "$region",
      totalRevenue: { $sum: "$amount" },
      avgOrderValue: { $avg: "$amount" },
      maxOrder: { $max: "$amount" },
      orderCount: { $count: {} }
  }}
])

4. Reshaping with $project

db.employees.aggregate([
  { $project: {
      fullName: { $concat: ["$firstName", " ", "$lastName"] },
      annualSalary: { $multiply: ["$monthlySalary", 12] },
      _id: 0
  }}
])
Performance Tip: Place $match and $sort as early as possible in the pipeline — this lets MongoDB use indexes and reduces the number of documents flowing into later stages.

5. A Complete Example

db.orders.aggregate([
  { $match: { orderDate: { $gte: ISODate("2026-01-01") } } },
  { $group: { _id: "$productCategory", totalSold: { $sum: "$quantity" } } },
  { $sort: { totalSold: -1 } },
  { $limit: 5 }
])
// Returns the top 5 best-selling categories year-to-date

6. Aggregation Pipeline Checklist

  • ✅ Chain $match, $group, $sort, $project confidently
  • ✅ Use accumulator operators — $sum, $avg, $max, $min
  • ✅ Reshape output fields with $project
  • ✅ Optimize pipelines by filtering early
Key Takeaway: The aggregation pipeline is MongoDB's answer to SQL's GROUP BY and analytical queries — and it's far more expressive once you're comfortable chaining stages together.

Ready to master MongoDB?

Build real-world MongoDB-powered applications with hands-on projects, mentor-led sessions, and placement support.

Explore Course