Master MongoDB's aggregation framework. Learn $match, $group, $sort, $project, $unwind, $lookup, $addFields, and $bucket to transform and analyze data at scale.
📊 Data Analytics🔄 Pipeline Stages⏱️ ~25 min
Lesson 25 of Databases & SQL Track
The Pipeline Concept
The aggregation pipeline is MongoDB's powerhouse for analytics. Instead of retrieving all data and processing it in your application, the pipeline transforms data inside MongoDB using stages that flow in sequence.
Think of it like a factory assembly line: raw documents enter Stage 1, get transformed, move to Stage 2, get transformed again, and so on. By the time data exits the pipeline, you have exactly what you need.
Pipeline Flow: $match → filters documents early (reduces data processed) → $group → aggregates by key → $sort → orders results → $project → selects final fields.
The key insight: $match early. Filtering documents at the start of the pipeline is much faster than grouping everything and then filtering.
Why Aggregation Matters
Efficiency: Processing happens on the database server, not your application.
Complex Queries: Group by multiple fields, compute running totals, reshape nested arrays.
Analytics: Revenue per category, top customers, supplier performance, all in one query.
Grouping & Accumulators
The $group stage is where the magic happens. It groups documents by a field and accumulates values using operators like $sum, $avg, $min, $max, $push, $first, and $last.
Q1: Count Products by Category
Let's count products in each category and calculate average price:
In MongoDB, documents can have nested arrays. The $unwind stage flattens an array field, creating one document per array element. This is perfect for analyzing items in orders.
Understanding $unwind
Imagine an order document with multiple items. $unwind creates a separate document for each item, allowing you to aggregate by product.
javascript
After $unwind:
Q4: Total Units Sold Per Product
Unwind orders, then group by product_id to sum quantities: