MongoDB — Finding Documents & Query Selectors
Master find() and findOne() methods. Learn equality filters, cursor manipulation, and how to inspect query execution plans with explain().
1Finding Documents with find() and findOne()
Basic Find Queries
// 1. Find the first matching document (returns plain document object or null)
db.users.findOne({ email: "guru@ourcompiler.com" });
// 2. Find all matching documents (returns a cursor)
db.users.find({ role: "admin" });
// 3. Find all documents in a collection
db.users.find({});
2Understanding MongoDB Query Cursors
The find() method does not immediately return all matching documents to memory. Instead, it returns a Cursor — an iterator that fetches documents in batches of 101 records (or 4 MB).
Cursor Methods
// Count total matching documents
db.users.find({ active: true }).count();
// Convert cursor result directly to a JavaScript Array
const activeUsers = db.users.find({ active: true }).toArray();