Question 6 / 50
ImportantEasyConceptCRUD
What is the difference between find() and findOne()?
Think about it first.
Short answer
find() returns a cursor you iterate; findOne() returns a single document or null. findOne is find with a limit of one, and it unwraps the result for you.
Why?
The cursor matters: find() fetches in batches as you iterate, so an unbounded find() is dangerous only when you materialise it into an array. findOne() returns null rather than an empty array, so the null check is different.
Example
db.users.findOne({ email: "rahul@example.com" })
db.users.find({ email: "rahul@example.com" }).limit(1)Interview tip
Mention that you would never call toArray() on an unfiltered find() in a web request.
Common mistake
Treating find() as 'returns an array'. In drivers it returns a cursor.
How did you do?