What is the aggregation pipeline in MongoDB?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid MongoDB basics — a prerequisite for any developer role.

Answer

The aggregation pipeline is MongoDB's framework for processing and transforming documents through a sequence of stages. Each stage transforms the documents and passes the output to the next stage — like a Unix pipe. Common stages: $match: filter documents (like WHERE) — place early to reduce data volume: { $match: { status: "active", age: { $gte: 18 } } }; $group: group documents by a key and compute aggregations: { $group: { _id: "$department", totalSalary: { $sum: "$salary" }, count: { $sum: 1 }, avgSalary: { $avg: "$salary" } } }; $project: reshape documents — include/exclude fields, add computed fields: { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] }, _id: 0 } }; $sort: sort documents: { $sort: { salary: -1 } }; $limit: return first N documents; $skip: skip first N documents (pagination); $unwind: deconstruct an array field into separate documents — one per array element; $lookup: left join with another collection; $addFields: add new fields; $count: count documents; $bucket / $bucketAuto: categorize into buckets (histogram); $facet: multiple aggregations on same data. Pipeline execution: MongoDB optimizes — pushes $match and $limit early; uses indexes where possible. db.orders.aggregate([ { $match: ... }, { $group: ... }, { $sort: ... } ])

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex MongoDB answers easy to follow.