close() (Close Cursor)
Last updated: 2026-10-09
close() (Close Cursor)
close() shuts down a cursor early and frees server-side resources: `db.users.find().close()`. Call it when you no longer need the remaining documents, stopping the server from streaming further batches. It returns null; closing an already-exhausted or closed cursor does not error.
| Category | Cursor |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.find(<filter>).close()
Returns:null.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// close: process the first doc, then close
const cursor = db.users.find()
if (cursor.hasNext()) {
print(cursor.next().name)
}
cursor.close()
Output:
Alice
📌 Notes
- Explicitly closing early releases server resources
- A closed cursor cannot be iterated
- mongosh and most drivers auto-close on exhaustion or GC
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QCan I iterate after close()?
ANo — after close() the cursor is unusable; re-run the query.
QWhat happens if I never call close()?
AUsually fine — cursors clean up on exhaustion or timeout; manual close is just tidier when exiting early.
QWhat does close() return?
AIt returns null; the result is usually ignored.