Cursor in MongoDB Tutorial: Learn with Examples

⚡ Smart Summary

Cursor in MongoDB is a pointer returned by the db.collection.find() function to the result set of matching documents. It iterates automatically by default, but developers can step through documents one by one using methods such as hasNext() and next().

  • 🖱️ Cursor Defined: A pointer to the documents returned by db.collection.find() in MongoDB.
  • 🔁 Auto-Iteration: By default the cursor iterates automatically when the query result is returned.
  • 👉 Manual Iteration: Step through documents one by one using hasNext() and next() in a while loop.
  • 📄 Readable Output: Use tojson() to print each document in readable JSON format.
  • 🔎 Example Query: find({Employeeid:{$gt:2}}) returns employees with an id greater than 2.

Cursor in MongoDB

What is Cursor in MongoDB?

When the db.collection.find () function is used to search for documents in the collection, the result returns a pointer to the collection of documents returned which is called a cursor.

By default, the cursor will be iterated automatically when the result of the query is returned. But one can also explicitly go through the items returned in the cursor one by one. If you see the below example, if we have 3 documents in our collection, the cursor object will point to the first document and then iterate through all of the documents of the collection.

Cursor in MongoDB

The following example shows how this can be done.

var myEmployee = db.Employee.find( { Employeeid : { $gt:2 }});

	while(myEmployee.hasNext())

	{

		print(tojson(myEmployee.next()));

	}

Code Explanation

  1. First we take the result set of the query which finds the Employee’s whose id is greater than 2 and assign it to the JavaScript variable ‘myEmployee’
  2. Next we use the while loop to iterate through all of the documents which are returned as part of the query.
  3. Finally for each document, we print the details of that document in JSON readable format.

If the command is executed successfully, the following Output will be shown

Output

Cursor in MongoDB

FAQs

You iterate a MongoDB cursor using methods like hasNext() and next() in a while loop, or with forEach(). By default, the shell automatically iterates the first 20 documents.

A MongoDB cursor returns documents in batches. The first batch holds up to 101 documents or 16 MB, and the shell auto-displays the first 20 results before prompting for more.

Yes. By default, a MongoDB cursor times out after 10 minutes of inactivity on the server. You can disable this with the noCursorTimeout option for long-running operations.

AI assistants can generate MongoDB find queries, build aggregation pipelines, explain cursor methods, and convert plain-language requests into shell or driver code, helping developers query data faster.

Yes. AI tools can suggest indexes, recommend projections to limit returned fields, and flag inefficient queries, helping reduce cursor processing time and memory usage in MongoDB applications.

Summarize this post with: