find() — MongoDB Technical Interview Questions & Answers
Queries documents matching a filter.
find()
- Simple meaning: find() returns a cursor of matching documents. Empty filter {} means all.
- How to use: db.students.find({ city: "Pune" }). Chain .sort({ marks: -1 }).limit(5).
- Pretty print: .pretty() in older mongo shell; mongosh often prints nicely already.
- Try this: find all, then find { marks: { $gt: 40 } }.
- Common mistake: expecting one object — find returns a cursor. Use findOne for one doc. Or toArray() in drivers.
- Interview tip: Filter document + cursor. Contrast findOne.
Technical Interview Tip: find({ marks: { $gt: 40 } }) and say it is a cursor.
Code Examples for find()
find() — working example
db.users.find(
{ age: { $gte: 18 }, city: "Pune" },
{ name: 1, email: 1, _id: 0 }
)find() — find returns a cursor of matches; first object is the filter, second is projection.
How to Explain find() to an Interviewer
What does find() return?
A cursor over matching documents