findOne() — MongoDB Technical Interview Questions & Answers
Returns the first document matching a filter.
findOne()
- Simple meaning: findOne returns the first matching document or null — not a cursor.
- How to use: db.students.findOne({ roll: 12 }).
- Try this: findOne missing roll — you get null. Good for “load this student”.
- Common mistake: findOne without filter when many docs exist — you get an arbitrary first document.
- Interview tip: One document or null. find() is many.
Technical Interview Tip: findOne({ roll: 12 }) vs find().
Code Examples for findOne()
findOne() — working example
db.users.findOne(
{ email: "ada@example.com" },
{ name: 1, email: 1, skills: 1, _id: 0 }
)findOne() — findOne returns a single document (or null), not a cursor.
How to Explain findOne() to an Interviewer
find vs findOne?
find = cursor of many findOne = one document or null