It processes documents through a pipeline of stages, where each stage transforms the documents before passing them to the next
The aggregation framework is MongoDB’s built-in way to do multi-document analysis like this — counting, grouping, transforming and filtering in one pipeline.
Aggregation Framework in MongoDB works very much like an RxJS pipeline, but for database documents. Pipeline stages are like RxJS operators, Documents flow through the pipeline like events through a stream and Each stage outputs a new transformed stream of documents to the next stage. In MongoDB’s Aggregation Framework, the stream is inside the database engine, so it’s usually much faster because it avoids network round-trips and leverages DB indexes.
- Purpose → Transform, filter, join, and analyze data inside MongoDB before it leaves the server.
- Scope → Operates on sets of documents, passing them through multiple stages in a pipeline.
- Capabilities:
- Grouping ($group) like SQL’s
GROUP BY
. - Filtering on group-level conditions ($match after $group) like SQL’s
HAVING
. - Sorting, reshaping, computing new fields ($project), joining collections ($lookup), array manipulation, text search scoring, geospatial ops, etc.
- Avoids pulling lots of raw data into your app just to process it there.
- Grouping ($group) like SQL’s
db.employees.aggregate([
{ $group: { _id: "$EmployeeId", count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } }
])
- ✅ Complex analysis in one trip to the DB.
- ✅ More efficient — processing happens server-side.
- ✅ Cleaner code — no need for multiple queries + app-side loops.
In short:
MongoDB Normal query = “Give me these documents as-is.”
Aggregation = “Take all the documents, reshape them, combine them, compute new info and give me only the final processed result.”
Can this catch the flip case where the name is passed as lastName + firstName.