Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Spring Data MongoDB

MongoDB Basics

  • Spring Data MongoDB
  • Indexing & Performance

Aggregation Pipeline

  • Aggregation Pipeline
  • Transactions in MongoDB
  • Schema Design Patterns
Chaturmind
← Spring Data MongoDB

MongoDB Basics

  • Spring Data MongoDB
  • Indexing & Performance

Aggregation Pipeline

  • Aggregation Pipeline
  • Transactions in MongoDB
  • Schema Design Patterns
HomeLearnSpring BootSpring Data & MongoDBAggregation
✓ FreeIntermediate· 14 min read

MongoDB Aggregation Pipeline

Build multi-stage aggregation pipelines with $match, $group, $lookup, $project, and $unwind.

Published April 9, 2025


MongoDB Aggregation Pipeline

The aggregation pipeline is MongoDB's answer to SQL GROUP BY, JOINs, and computed columns. Data flows through a sequence of stages, each transforming the documents.

Core Stages

db.orders.aggregate([
  // Stage 1: filter (like WHERE)
  { $match: { status: 'COMPLETED', createdAt: { $gte: new Date('2024-01-01') } } },

  // Stage 2: group (like GROUP BY + aggregate functions)
  { $group: {
      _id: '$userId',
      totalRevenue: { $sum: '$total' },
      orderCount:   { $sum: 1 },
      avgOrderValue: { $avg: '$total' },
      lastOrder:    { $max: '$createdAt' }
  }},

  // Stage 3: compute new fields
  { $addFields: {
      isHighValue: { $gte: ['$totalRevenue', 1000] }
  }},

  // Stage 4: sort
  { $sort: { totalRevenue: -1 } },

  // Stage 5: limit
  { $limit: 10 },

  // Stage 6: reshape output
  { $project: {
      userId: '$_id',
      totalRevenue: 1,
      orderCount: 1,
      isHighValue: 1,
      _id: 0
  }}
])

$lookup — JOIN in MongoDB

db.orders.aggregate([
  { $match: { userId: 'u123' } },

  // Join with users collection
  { $lookup: {
      from: 'users',
      localField: 'userId',
      foreignField: '_id',
      as: 'userDetails'
  }},

  // $lookup produces an array — unwind to flatten
  { $unwind: '$userDetails' },

  { $project: {
      orderId: '$_id',
      userName: '$userDetails.name',
      total: 1
  }}
])

$unwind — flatten arrays

// Document: { _id: 1, tags: ['java', 'spring', 'api'] }
db.posts.aggregate([
  { $unwind: '$tags' }
])
// Produces 3 documents: one per tag
// { _id: 1, tags: 'java' }
// { _id: 1, tags: 'spring' }
// { _id: 1, tags: 'api' }

$facet — multiple aggregations in parallel

db.products.aggregate([
  { $facet: {
      byCategory: [
          { $group: { _id: '$category', count: { $sum: 1 } } }
      ],
      priceStats: [
          { $group: { _id: null, avg: { $avg: '$price' }, max: { $max: '$price' } } }
      ],
      topProducts: [
          { $sort: { sales: -1 } },
          { $limit: 5 }
      ]
  }}
])

Performance Tips

  1. Put $match and $limit as early as possible — reduces documents flowing through later stages
  2. $match on indexed fields — MongoDB can use the index before loading documents
  3. $project early — drop unneeded fields to reduce memory usage
  4. allowDiskUse: true for pipelines that exceed the 100MB memory limit
db.orders.aggregate([...], { allowDiskUse: true })

Common Real-World Pipelines

// Daily revenue report
db.orders.aggregate([
  { $match: { status: 'COMPLETED' } },
  { $group: {
      _id: { $dateToString: { format: '%Y-%m-%d', date: '$createdAt' } },
      revenue: { $sum: '$total' },
      orders: { $sum: 1 }
  }},
  { $sort: { _id: 1 } }
])

Interview Tips

  1. Explain why $match before $group is critical for performance — it reduces the input set.
  2. Know that $lookup is a left outer join by default.
  3. For Spring Boot: use MongoTemplate.aggregate() or @Aggregation annotation in Spring Data.

Previous

Indexing & Performance

Next

Transactions in MongoDB

AI Tutor

Lesson: MongoDB Aggregation Pipeline

Quick actions

AI responses can be inaccurate. Verify critical information.