0

This is from a book I am reading:

Strictly speaking, the find() command returns a cursor to the returning documents. Therefore, to access the documents you’ll need to iterate the cursor. The find() command automatically returns 20 documents—if they’re available—after iterating the cursor 20 times.

I cannot understand what the author means. What is a cursor in MongoDB.

Aditya Raj
  • 327
  • 3
  • 14

1 Answers1

0

There are many slightly different ways to process the result of a request:

  • Maybe you want to sort them
  • Maybe you want to limit the number of results
  • Maybe you want to skip items
  • Etc...

To allow you to do this in a convenient way, and offer a performant implementation, mongodb asks you to do things in two steps:

  1. Speficy the request (filter and projection)
  2. Then, tell what you want to do with the results (sort, skip, limit, etc...)

Step 1 returns the cursor.

The cursor has methods which allow you to specify what you want to do in step 2, and it also has methods which allow you to iterate on the result.

Results are actually retrived over time while you iterate. This allows to use reasonable amounts of system resources.

Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90