Aggregation Pipeline Basics
Sometimes you need more than just finding documents – you need to analyze data, calculate totals, group by categories, or transform results. MongoDB's **aggregation pipeline** is like an assembly line that processes documents through multiple stages to produce computed results.
What is the Aggregation Pipeline?
The aggregation pipeline is a framework for data aggregation. Documents pass through multiple stages, and each stage transforms the documents. It's similar to Unix pipes (`|`) where you pass output from one command to the next.
Think of the aggregation pipeline as a factory assembly line. Raw materials (documents) go in, pass through different stations (stages), and come out as finished products (results).
Basic Aggregation Syntax
db.collection.aggregate([ { stage1 }, { stage2 }, { stage3 }])Common Aggregation Stages
| Stage | Description |
|---|---|
| `$match` | Filters documents (like find) |
| `$group` | Groups documents by a key and computes aggregates |
| `$project` | Selects/shapes fields (like projection) |
| `$sort` | Sorts documents |
| `$limit` | Limits number of documents |
| `$unwind` | Deconstructs an array field |
Sample Data for Examples
db.orders.insertMany([ { product: "Laptop", category: "electronics", price: 999, quantity: 2 }, { product: "Mouse", category: "electronics", price: 25, quantity: 5 }, { product: "Book", category: "books", price: 15, quantity: 10 }, { product: "Headphones", category: "electronics", price: 50, quantity: 3 }, { product: "Desk", category: "furniture", price: 200, quantity: 1 }, { product: "Notebook", category: "books", price: 5, quantity: 20 }])Example 1: $match – Filtering
<!-- Get all electronics orders -->db.orders.aggregate([ { $match: { category: "electronics" } }]).pretty()Example 2: $group – Grouping and Calculating
<!-- Calculate total sales by category -->db.orders.aggregate([ { $group: { _id: "$category", <!-- Group by category --> totalQuantity: { $sum: "$quantity" }, totalRevenue: { $sum: { $multiply: ["$price", "$quantity"] } }, averagePrice: { $avg: "$price" }, count: { $sum: 1 } } }]).pretty()Output:
{ "_id": "electronics", "totalQuantity": 10, "totalRevenue": 2198, "averagePrice": 358, "count": 3}Example 3: $project – Shaping Results
<!-- Show product name, price, and a calculated discount price -->db.orders.aggregate([ { $project: { product: 1, price: 1, discountPrice: { $multiply: ["$price", 0.9] }, priceCategory: { $cond: { if: { $gte: ["$price", 100] }, then: "expensive", else: "cheap" } } } }]).pretty()Example 4: Multiple Stages Together
<!-- Get top 2 categories by revenue, show only category and revenue -->db.orders.aggregate([ { $group: { _id: "$category", revenue: { $sum: { $multiply: ["$price", "$quantity"] } } } }, { $sort: { revenue: -1 } }, { $limit: 2 }, { $project: { _id: 0, category: "$_id", revenue: 1 } }]).pretty()Two Minute Drill
- Aggregation pipeline processes documents through multiple stages.
- `$match` filters documents (like find).
- `$group` groups documents and computes aggregates (sum, avg, count).
- `$project` shapes the output (rename fields, add computed fields).
- You can combine stages in any order – the output of one stage becomes input to the next.
- Aggregation is powerful for data analysis, reports, and complex queries.
Need more clarification?
Drop us an email at career@quipoinfotech.com
