MongoDB Cursor Methods

Some of the commonly used cursor methods are:

Count cursor:

In order to get the correct documents, we need to know how many documents are present for that collection. To get that we can use the count() method which returns the total number of documents present in the given collection.

Syntax:

db.collection_name.find().count()
or 
db.collection_name.count()

Example: 

db.student.find().count()

Here, we find the total number of documents present in the student collection using the count() method.

Cursor Limit:

The limit() method helps to fetch limited records from a collection. Suppose we have multiple documents, but we want to have the topmost or only 2 documents, then by using the limit() method, we can achieve that.

Syntax:

db.collection_name.find().limit(<number>)

Example:

db.student.find().limit(2).pretty()

Here, we only display the first two documents from the student collection.

Cursor size:

The cursor.size() method will be helpful to return a count of the number of documents that got as the output from the db.collection.find() query after applying any cursor.skip() and cursor.limit() methods. Hence, it is mentioned as it has applied cursor.skip() and cursor.limit() methods.

Syntax:

db.collection_name.find().size()

Example:

db.student.find({studentId:1}).size()

Cursor sort:

Usually while verifying documents, if the output is in sorted order, either in ascending or descending order, it will be easier. So we use the sort() method to sort the documents. If you want to sort the documents in ascending, then set the value of the field to 1 and in descending, then set -1.

Syntax:

db.collection_name.find().sort(<sort>)

Example:

db.student.find().sort({studentId:-1}).pretty()

Here, we sort all the documents present in the student collection in descending order.

Cursor.toArray():

In order to have an array that contains all documents returned by the cursor, we can use the toArray() method.

Syntax:

db.collection_name.find().toArray()

Example:

db.student.find().toArray()

Cursor.next:

The next() method is used to return the next document in a cursor. Usually, it will return the first document as that will be the result of the first document in the cursor.

Syntax:

db.student.find().next()

Example:

db.student.find().next()

Here, the next() method returns the first document from the collection.

MongoDB Cursor

The MongoDB cursor is a pointer that references the documents of the collection returned by the find() method.

The cursor is used to access the documents. By default, the cursor iterates automatically, but can also be iterated manually by the user.

Similar Reads

MongoDB Cursor Example

In this example, we are working with:...

How to Manually Iterate a Cursor in MongoDB

There are different ways to manually iterate a cursor in MongoDB. Users can manually iterate a cursor in MongoDB by:...

MongoDB Cursor Methods

Some of the commonly used cursor methods are:...

Additional Cursor Methods in MongoDB

Cursor Methods modify the way that the underlying query is executed....

Conclusion

Cursors in MongoDB reference the documents of a collection returned by the find() method. A cursor is used to iterate over documents when the query result is returned....

Contact Us