What is the difference between find() and aggregate() in MongoDB?
Understand the difference between MongoDB find() and aggregate(): simple filtered reads versus multi-stage pipelines that group, join and compute derived data.
Expected Interview Answer
find() retrieves documents that match a filter and optionally projects or sorts them, while aggregate() runs documents through a multi-stage pipeline that can filter, group, reshape, join, and compute derived values.
find() is ideal for straightforward reads where you want raw matching documents back. aggregate() is a data-processing framework: each stage ($match, $group, $project, $lookup, $sort, $unwind, and more) transforms the stream of documents and passes the result to the next stage. Anything find() can do, aggregate() can do with $match and $project, but aggregate() adds grouping, aggregation functions like $sum and $avg, joins across collections, and complex transformations that find() cannot express.
- find() is simpler and faster for plain lookups
- aggregate() supports grouping and computed metrics
- aggregate() can join collections with $lookup
- Pipelines reshape documents stage by stage
- aggregate() handles analytics find() cannot
AI Mentor Explanation
find() is like pulling every delivery a bowler bowled straight from the scorebook — you get the raw balls that match. aggregate() is the analyst who feeds those deliveries through stages: filter to death overs, group by bowler, compute economy rate, then sort by best figures. find() hands you the raw records; aggregate() runs them through a processing line to produce insights the scorebook never stored directly.
Step-by-Step Explanation
Step 1
Use find() for simple reads
Pass a filter document and optional projection: db.orders.find({ status: 'shipped' }, { total: 1 }).
Step 2
Reach for aggregate() when you need more
Use it whenever you must group, join, or compute derived values that find() cannot express.
Step 3
Build a pipeline as stages
aggregate() takes an ordered array of stage objects, each transforming the document stream.
Step 4
Filter early with $match
Put $match first so later stages process fewer documents and indexes are used.
Step 5
Group and compute
Use $group with accumulators like $sum and $avg to produce aggregated results.
What Interviewer Expects
- Clear statement that aggregate() is a multi-stage pipeline
- Examples of stages ($match, $group, $project, $lookup)
- Knowing aggregate() can do everything find() can plus more
- When find() is the simpler, faster choice
- Awareness of putting $match early for performance
Common Mistakes
- Saying find() can do grouping and aggregation
- Using aggregate() for trivial lookups find() handles better
- Placing $match late so more documents are processed
- Confusing $project in aggregate() with find() projection semantics
- Not knowing $lookup enables joins across collections
Best Answer (HR Friendly)
“find() simply returns the documents that match your criteria, like a basic search. aggregate() is a more powerful tool that runs data through several processing steps to group, calculate, and reshape it, so it can produce summaries and reports that a plain search cannot.”
Code Example
// find(): raw documents matching a filter
db.orders.find({ status: 'shipped' }, { customerId: 1, total: 1 })
// aggregate(): group and compute total revenue per customer
db.orders.aggregate([
{ $match: { status: 'shipped' } },
{ $group: { _id: '$customerId', revenue: { $sum: '$total' } } },
{ $sort: { revenue: -1 } }
])Follow-up Questions
- What does the $group stage do and what accumulators can it use?
- How does $lookup perform a join between collections?
- Why should $match come early in an aggregation pipeline?
- What is the difference between $project in find() and in aggregate()?
- When would you prefer find() over aggregate() for performance?
MCQ Practice
1. Which operation can group documents and compute a $sum?
Grouping and accumulators like $sum are features of the aggregation pipeline's $group stage, not find().
2. Which aggregation stage should usually come first for performance?
Placing $match early filters documents up front (and can use indexes), so later stages process fewer documents.
3. Which stage joins data from another collection?
$lookup performs a left outer join, pulling matching documents from another collection into the pipeline.
Flash Cards
What does find() return? — The documents matching a filter, optionally projected and sorted — no grouping or computation.
What is aggregate()? — A multi-stage pipeline that filters, groups, joins, reshapes, and computes over documents.
What does $group do? — Groups documents by a key and computes accumulators like $sum, $avg, and $count.
What does $lookup do? — Performs a left outer join, bringing matching documents from another collection into the pipeline.