What is the difference between find() and findOne()?
Both retrieve documents from a collection but differ in what they return.
find(filter) — returns a cursor over all matching documents. Iterate to consume; supports chaining
.sort(),.limit(),.skip().
findOne(filter) — returns the first matching document directly (or
null). No cursor.
Use findOne when you expect a single result (lookup by ID); use find when iterating multiple matches.
// find() — returns cursor
const cursor = db.users.find({ age: { $gt: 25 } });
cursor.sort({ age: -1 }).limit(10).skip(20); // Chainable
cursor.forEach(doc => console.log(doc.name)); // Iterate
// Convert cursor to array
const users = db.users.find({ active: true }).toArray();
// findOne() — returns single document
const user = db.users.findOne({ _id: ObjectId('507f1f77bcf86cd799439011') });
if (user) console.log(user.name); // Direct access, no cursor
// findOne returns null if not found
const missing = db.users.findOne({ email: 'nonexistent@test.com' });
console.log(missing); // nullfind returns a cursor that supports sort/limit/skip chaining. toArray() materializes the cursor into an array. findOne returns the document directly (or null). findOne is cleaner for single-document lookups (no cursor wrapping).
find = cursor (many results, chainable). findOne = single document or null (no cursor). find().limit(1) is similar but returns a cursor. findOne is preferred for by-_id lookups. Cursors are lazy (batch fetching) — important for large result sets.