Aggregation Framework
Aggregation Framework
Section titled “Aggregation Framework”Aggregation is MongoDB’s pipeline system for transforming, joining, grouping, and analyzing documents.
Think of it as:
flowchart LR Docs[(Collection Raw Documents)] --> S1["Stage 1: $match Filter documents"] S1 --> S2["Stage 2: $group Group & aggregate"] S2 --> S3["Stage 3: $sort Sort results"] S3 --> Result["📊 Final Result Transformed data"]
style Docs fill:#3b82f6,color:#fff style S1 fill:#7c3aed,color:#fff style S2 fill:#059669,color:#fff style S3 fill:#f59e0b,color:#fff style Result fill:#10b981,color:#fffEach stage receives documents from the previous stage and sends transformed documents to the next stage.
documents -> stage -> stage -> stage -> resultSections
Section titled “Sections”- Pipeline Basics & Stages
- $match, $project, $sort, $limit, $skip
- $group & Accumulators
- $lookup & $unwind
- Expressions & Computed Fields
- Real-World Examples
- Aggregation Optimization
Basic Syntax
Section titled “Basic Syntax”db.collection.aggregate([ { $match: { status: "completed" } }, { $group: { _id: "$userId", total: { $sum: "$totalAmount" } } }, { $sort: { total: -1 } }])Most Common Stages
Section titled “Most Common Stages”| Stage | Purpose |
|---|---|
$match | Filter documents |
$project | Include, exclude, rename, or compute fields |
$group | Group documents and calculate totals |
$sort | Sort result documents |
$limit | Keep only a fixed number of documents |
$skip | Skip documents, often for pagination |
$lookup | Join with another collection |
$unwind | Flatten array values |
$addFields | Add computed fields |