Cursor in MongoDB

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:04:00

What is a Cursor?

A cursor is a pointer to a collection of documents returned by the find() method. It is automatically iterated when the query results are delivered. However, you can navigate through the objects returned in a cursor one by one.

Iterating a Cursor Manually in MongoDB

There are several ways to manually iterate a cursor in MongoDB. You can manually iterate a cursor in MongoDB by: 

  • Assigning it to a Var Keyword
  • Using the next() method
  • Using the forEach() function
  • Using the Iterator Index

Assigning it to a Var Keyword

By assigning the cursor to a variable, you can iterate through its documents using typical control flow constructs such as loops or conditional expressions.

Using the next() function

This method moves the cursor to the next document in the result set, allowing you to navigate the cursor's documents in a sequential order.

Using the forEach() function

MongoDB has a forEach() function that can be applied directly to the cursor, executing a specified function for each document in the cursor.

Using the Iterator Index

MongoDB also supports accessing documents depending on their index position within the cursor, which allows for random access to elements in the result set.

Cursor Methods

  • cursor.addOption(): Adds specific wire protocol flags that change query behavior, allowing for more sophisticated query customization.
  • cursor.allowDiskUse(): Allows MongoDB to use temporary disc storage for data that exceeds the system memory limit during sort operations.
  • cursor.allowPartialResults(): Allows queries on sharded collections to return partial results if some shards are unavailable, rather than throwing an error.
  • cursor.batchSize(): Determines the number of documents returned in each network message, hence optimizing network traffic utilization.
  • cursor.close(): Closes the cursor by freeing the server resources connected with it.
  • cursor.isClosed(): Determines whether the cursor is closed.
  • cursor.collation(): Sets the collation for the cursor's find operation, which affects string comparison.
  • cursor.comment(): Adds a comment to the query for traceability in logs and profiling.
  • cursor.count(): Sets the cursor to return the number of documents in the result set rather than the documents themselves.
  • cursor.explain(): Provides information about the query execution plan.
  • cursor.forEach(): Executes a JavaScript function for each document in the cursor.
  • cursor.hasNext(): Determines whether there are more documents to iterate over.
  • cursor.hint(): Directs MongoDB to use a certain index for the query.
  • cursor.isExhausted(): Determines whether the cursor is closed with no lingering objects.
  • cursor.itcount(): Calculates the total number of documents in the cursor on the client side.
  • cursor.limit(): Limits the size of the cursor's results collection.
  • cursor.map(): Apply a function to each document and save the results in an array.
  • cursor.max(): Sets an upper index bound for the cursor.
  • cursor.maxAwaitTimeMS(): Sets a time limit for the next query result update.
  • cursor.maxTimeMS(): Sets a cumulative time limit for cursor operations.
  • cursor.min(): Sets a lower index bound for the cursor.
  • cursor.next(): Returns the next document in the cursor.
  • cursor.noCursorTimeout(): Prevents the server from immediately closing the cursor due to inactivity.
  • cursor.objsLeftInBatch(): Returns the number of documents remaining in the current cursor batch.
  • cursor.pretty(): Sets the cursor to display results in legible format.
  • cursor.readConcern(): Sets the read concern for the find operation.
  • cursor.readPref(): Configures a read preference to determine how queries are routed to a replica set.
  • cursor.returnKey(): Sets the cursor to return index keys rather than documents.
  • cursor.showRecordId(): Each document has an internal storage engine ID field.
  • cursor.size(): Returns the number of documents after applying skip() and limit().
  • cursor.skip(): Skips a given number of documents in the cursor.
  • cursor.sort(): Sorts results based on a provided specification.
  • cursor.tailable(): Marks the cursor as tailable; this only applies to cursors over capped collections.
  • cursor.toArray(): Converts the cursor into an array of all returned documents.
  • cursor.tryNext(): If the next element in the iteration is accessible, return it; otherwise, return null.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this