What does "aggregation pipeline" mean?
An aggregation pipeline is a data-processing conveyor in MongoDB, where documents pass through a sequence of stages, each one transforming the data step by step: filtering, sorting, grouping, projecting, and so on.
The key ideas
- A pipeline is a chain of stages Data flows top to bottom, like through pipes. One stage's output is the next stage's input.
- Every stage does one clear thing For example:
$match, a filter$group, grouping and aggregates$sort, sorting$project, picking and changing fields
- Everything happens on MongoDB's side So you're not pulling a lot of data into your code, you're processing it right inside the DB, which is faster and more efficient.
Example
js
db.orders.aggregate([
{ $match: { status: "paid" } },
{ $group: { _id: "$user_id", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } }
]);- first a filter by
status - then a sum per user
- then sorting by the total
Summary
An aggregation pipeline is a mechanism where data passes through a chain of stages, gets transformed step by step, and produces a finished analytical result in a single query.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.