When you run find(), MongoDB does not immediately return all matching documents. Instead, it returns a cursor — a pointer to the result set stored server-side. Documents are fetched in batches as you iterate.
Why Cursors Exist
- Avoid loading millions of documents into memory at once
- Enable streaming/lazy consumption — process one batch at a time
- Allow cancellation mid-way through a large result set
- Support chaining of
sort(),limit(),skip()before execution
Cursor in the Shell
// find() returns a cursor — not documents directly const cursor = db.users.find({ active: true }) // The mongosh shell auto-iterates and prints the first batch db.users.find({ active: true }) // Prints first 20 documents, then shows "Type 'it' for more" // "it" iterates the cursor to the next batch // In Node.js — cursor is not consumed until you iterate const cursor = collection.find({ active: true }) // Nothing has been sent to the DB yet in terms of data transfer for await (const doc of cursor) { console.log(doc) // fetches in batches as the loop runs }
it (iterate) to get the next batch. In drivers (Node.js, Python, etc.), you must explicitly iterate — the cursor does nothing on its own until consumed.