Navigating MongoDB Data with Cursors: A Comprehensive Guide

Learn how to effectively use cursors in MongoDB to iterate through query results. Discover essential cursor methods and techniques for efficient data retrieval and manipulation. This tutorial will provide you with a solid understanding of cursors and their role in MongoDB development.



MongoDB Cursor

The find() method returns a cursor object that can be used to iterate through the results. Here's how you can use it:

Example: Cursor Object Copy

var cursor = db.employees.find()

Important Cursor Methods

Method Description
cursor.count() Returns the total number of documents referenced by the cursor.
cursor.forEach() Applies a JavaScript function to each document from the cursor.
cursor.hasNext() Returns true if the cursor can iterate further to return more documents.
cursor.isExhausted() Returns true if the cursor is closed and there are no remaining objects in the batch.
cursor.itcount() Counts the number of documents remaining in the cursor.
cursor.limit() Specifies the maximum number of documents the cursor will return.
cursor.map() Applies a function to each document visited by the cursor and collects the return values.
cursor.max() Specifies the exclusive upper bound for a specific index to constrain the results of find().
cursor.min() Specifies the inclusive lower bound for a specific index to constrain the results of find().
cursor.next() Returns the next document from the result set.
cursor.pretty() Displays the result in a readable format.
cursor.readConcern() Specifies a level of isolation for read operations.
cursor.skip() Skips the specified number of documents for pagination.
cursor.sort() Specifies the order in which the query returns matching documents.
cursor.toArray() Returns an array containing all documents from the cursor.

Note: Cursor methods may vary depending on the drivers you are using in your application. Visit the MongoDB documentation for more details.

Example Usage of next() Method

humanResourceDB> var cur = db.employees.find()
humanResourceDB> cur.next()
{
_id:1,
firstName: "John",
lastName: "King",
email: "john.king@abc.com",
salary: 5000
}
humanResourceDB> cur.next()
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "sachin.t@abc.com",
salary: 8000
}
humanResourceDB> cur.next()
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "jamesb@abc.com",
salary: 7500
}
humanResourceDB> cur.next()
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "steve.j@abc.com",
salary: 7000
}
humanResourceDB> cur.next()
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "kapil.d@abc.com",
salary: 4500
}
humanResourceDB> cur.next()
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "amitabh.b@abc.com",
salary: 7000
}
humanResourceDB> cur.next()
null
humanResourceDB> cur.next()
MongoCursorExhaustedError: Cursor is exhausted

Using cursor.forEach() Method

humanResourceDB> var cur = db.employees.find()
humanResourceDB> cur.forEach(printjson)
{
_id:1,
firstName: "John",
lastName: "King",
email: "john.king@abc.com",
salary: 5000
}
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "sachin.t@abc.com",
salary: 8000
}
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "jamesb@abc.com",
salary: 7500
}
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "steve.j@abc.com",
salary: 7000
}
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "kapil.d@abc.com",
salary: 4500
}
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "amitabh.b@abc.com",
salary: 7000
}

Learn how to sort documents using cursor.sort() method in the next chapter.