find() vs aggregate() — MongoDB Technical Interview Questions & Answers
Side-by-side comparison table.
find() vs aggregate()
- Simple meaning: find() filters and projects documents. aggregate() can group, join, reshape through stages.
- How to use: list Pune students → find. Average marks per city → aggregate $group.
- find is simpler and enough for most CRUD screens. Aggregation is for reports.
- Common mistake: a 6-stage pipeline when find + sort + limit would do.
- Interview tip: find = query. aggregate = pipeline/report.
Technical Interview Tip: When would you switch from find to aggregate?
Code Examples for find() vs aggregate()
find() vs aggregate() — working example
db.orders.aggregate([
{ $match: { status: "paid" } },
{ $group: { _id: "$city", total: { $sum: "$amount" }, count: { $sum: 1 } } },
{ $project: { _id: 0, city: "$_id", total: 1, count: 1 } },
{ $sort: { total: -1 } }
])find() vs aggregate() — aggregation pipelines transform documents stage by stage ($match → $group → $project → $sort).
How to Explain find() vs aggregate() to an Interviewer
find vs aggregate?
find returns documents aggregate transforms through stages