Usage of Cursor in MongoDB with Examples

Using Cursor and its Methods within MongoDB:

Hope you are enjoying the range of tutorials in this Beginners Guide to MongoDB training series.

Our previous tutorial in this series explained all about find() method in MongoDB collection.

In this tutorial, we will learn about the cursor and its methods within MongoDB in detail.

Cursor in MongoDB

Cursor

The Cursor is a MongoDB Collection of the document which is returned upon the find method execution.

By default, it is automatically executed as a loop. However, we can explicitly get specific index document from being returned cursor. It is just like a pointer which is pointing upon a specific index value.

In simple words when we call a find method, all the documents which are returned are saved in a virtual cursor. If a find method returns for a document then it is mean that the cursor has 0 – 3 index.

Illustrate Cursor

In order to understand cursor, let's see an example.

In this Example, we will first check how many documents we have in our MongoDB collection. Then we will call a specific index which is returned by a cursor upon the execution of the find method with the following code.

Code

db.staff().find()

Figure 1: In Mongo Shell

Illustrate Cursor in mongo db

Figure 2: In Robo 3T

Illustrate Cursor in robo 3t

In the above example, it is clearly illustrated that the cursor is just like an array of the pointer which is pointing on a specific index. When we call index 0 by find method, then it returns us the first record which is placed on 0 indexes.

Count Cursor

Count method will help us to retrieve how many records are available within a cursor. If we will place “.count()” right after the find method then it will count how many records are there.

You can observe the following Example.

Code

db.staff().find().count()

Figure 3: In Mongo Shell

Count Cursor in mongo db

Figure 4: In Robo 3T

Count Cursor in robo 3t

Cursor Limit

A platform provides the MongoDB limit method by which we can fetch limited records from a cursor. A cursor has thousands of documents, but we need only 2 documents so that we can limit the cursor up to 2 documents as you can see in the following Example.

Code

db.staff().find().limit(2)

Figure 5: In Mongo Shell

Cursor Limit in mongo db

Figure 6: In Robo 3T

Cursor Limit in robo 3t

Cursor Prettify

It is used to make the fetched document readable. When we execute the find method, it will return all the available documents row by row. When we append pretty method with the find method, then it will return all the documents in the form of JSON which makes data easily understandable.

Code

db.staff().find().pretty()

Figure 7: In Mongo Shell

Cursor Prettify in mongo db

Figure 8: In Robo 3T

Cursor Prettify in robo 3t

Conclusion

The cursor is a pointer which is used to return all the fetched record. We can explicitly call a specific index value from a cursor. We can set a limit of the cursor and count its index. We can also prettify the fetched records in order to read them easily.

In simple words, we can use it to menu play the record according to our requirements.


Our upcoming tutorial will explain all about Query Modification in MongoDB using limit!!

PREV Tutorial | NEXT Tutorial