{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreigs2rz33jlvzgxq7whgsditqde2ghnzdnvarm4bgbr7bt5v5a6nhi",
"uri": "at://did:plc:5sgu76a53rz3n6unbykmovqy/app.bsky.feed.post/3mloh7lhfad62"
},
"description": "The Aggregation Pipeline is MongoDB's framework for transforming and combining documents through a sequence of stages, conceptually similar to a SQL SELECT with GROUP BY, JOIN, and window functions. A pipeline is an array of stage operators applied in order; the output of one stage feeds the next.\n\n\nCommon stages\n\n * $match: filter documents (equivalent to WHERE)\n * $project: reshape and select fields (equivalent to SELECT)\n * $group: group and aggregate (equivalent to GROUP BY)\n * $sort: sort d",
"path": "/engineering-glossary/aggregation-pipeline-mongodb-queries/",
"publishedAt": "2026-05-12T18:46:38.000Z",
"site": "https://sahilkapoor.com",
"tags": [
"MongoDB",
"Document Database",
"NoSQL",
"SQL",
"BSON",
"MongoDB Data Modeling: How to Design Schemas for Real-World Applications"
],
"textContent": "**The Aggregation Pipeline** is MongoDB's framework for transforming and combining documents through a sequence of stages, conceptually similar to a SQL `SELECT` with `GROUP BY`, `JOIN`, and window functions. A pipeline is an array of stage operators applied in order; the output of one stage feeds the next.\n\n## Common stages\n\n * `$match`: filter documents (equivalent to `WHERE`)\n * `$project`: reshape and select fields (equivalent to `SELECT`)\n * `$group`: group and aggregate (equivalent to `GROUP BY`)\n * `$sort`: sort documents (equivalent to `ORDER BY`)\n * `$limit`, `$skip`: pagination\n * `$lookup`: left outer join to another collection\n * `$unwind`: explode array fields into one document per element\n * `$facet`: run multiple sub-pipelines in parallel on the same input\n * `$graphLookup`: recursive graph traversal\n * `$merge`, `$out`: write results to another collection\n\n\n\n## Performance considerations\n\n * Place `$match` and `$project` early to reduce documents and fields flowing downstream.\n * Use indexes to support early `$match` and `$sort` stages.\n * `$lookup` is expensive; pre-denormalising data sometimes beats join-style queries.\n * Use `explain()` to inspect the executed plan and see which stages used indexes.\n\n\n\nš\n\n**Related Terms**\nMongoDB, Document Database, NoSQL, SQL, BSON\n\nš\n\n**Further Reading**\nMongoDB Data Modeling: How to Design Schemas for Real-World Applications",
"title": "Aggregation Pipeline",
"updatedAt": "2026-05-13T19:11:05.358Z"
}