MongoDB Tutorial
- Home
- Introduction
- Installation
- Connection & Databases
- CRUD Operations
- Comparison Operators
- Logical Operators
- Array Operators
- Evaluation Operators
- Update Operators
- Projection & Sorting
- Cursor Methods
- Querying Nested Docs
- Upserts & Indexes
- Aggregation Framework
- Data Modeling
- Validations & Transactions
- Geospatial Queries
- Advanced Indexing
- MongoDB with Next.js
- Best Practices
Aggregation Framework
Aggregation operations process data records and return computed results. In MongoDB, you use the Aggregation Pipeline to transform and filter data through multiple stages.
The Pipeline Concept
Imagine a factory assembly line. Documents go into the start of the pipeline and are modified or filtered at each stage until you get the final result.
db.orders.aggregate([
// Stage 1: Filter documents
{ $match: { status: "A" } },
// Stage 2: Group by product and calculate total quantity
{ $group: { _id: "$item", totalQty: { $sum: "$qty" } } },
// Stage 3: Sort by total quantity descending
{ $sort: { totalQty: -1 } }
])Common Pipeline Stages
| Stage | Description |
|---|---|
$match | Filters documents (similar to `find()`). |
$group | Groups documents by a specific key and performs accumulations (sum, avg, etc.). |
$project | Reshapes documents (renames fields, calculates new fields, removes fields). |
$sort | Sorts documents. |
$limit / $skip | Limits or skips a specified number of documents. |
$unwind | Deconstructs an array field from the input documents to output a document for each element. |
$lookup | Performs a left outer join to another collection in the same database. |
Complex Example: $lookup
The `$lookup` stage allows you to join two collections together.
db.orders.aggregate([
{
$lookup: {
from: "inventory", // Join with collection "inventory"
localField: "item", // field from orders
foreignField: "sku", // field from inventory
as: "inventory_docs" // output array field
}
}
])Pro Tip: Use `$match` as early as possible in the pipeline to reduce the number of documents being processed in subsequent stages.