The collection's find
method returns a cursor - this points to the set of documents (called as result set) that are matched to the query filter. The result set is the actual documents that are returned by the query, but this is on the database server.
To the client program, for example the mongo
shell, you get a cursor. You can think the cursor is like an API or a program to work with the result set. The cursor has many methods which can be run to perform some actions on the result set. Some of the methods affect the result set data and some provide the status or info about the result set.
As the cursor maintains information about the result set, some information can change as you use the result set data by applying other cursor methods. You use these methods and information to suit your application, i.e., how and what you want to do with the queried data.
Working on the result set using the cursor and some of its commonly used methods and features from mongo
shell:
The count()
method returns the count of the number of documents in the result set, initially - as the result of the query. It is always constant at any point in the life of the cursor. This is information. This information remains same even after the cursor is closed or exhausted.
As you read documents from the result set, the result set gets exhausted. Once completely exhausted you cannot read any more. The hasNext()
tells if there are any documents available to be read - returns a boolean true or false. The next()
returns a document if available (you first check with hasNext
, and then do a next
). These two methods are commonly used to iterate over the result set data. Another iteration method is the forEach()
.
The data is retrieved from the server in batches - which has a default size. With the first batch you read the documents and when all it's documents are read, the following next()
method retrieves the next batch of documents, etc., until all documents are read from the result set. This batch size can be configured and you can also get its status.
If you apply the toArray()
method on the cursor, then all the remaining documents in the result set are loaded into the memory of your client computer and are available as a JavaScript array. And, the result set data is exhausted. The following hasNext
method will return false
, and the next
will throw an error (once you exhaust the cursor, you cannot read data from it). This method loads all the result set data into your client's memory (the array). This can be memory consuming in case of large result sets.
The itcount()
returns the count of remaining documents in the result set and exhausts the cursor.
There are cursor methods like isClosed()
, isExhausted()
, size()
which give status information about the cursor and its underlying result set as you work with your data.
Those are the basic features of cursor and result set. There are many cursor methods, and you can try and see how they work and get a better understanding.
Reference:
Example usage in mongo
shell:
Assume the test
collection has 200 documents (run the commands in the same sequence).
var cur = db.test.find( { } ).limit(25)
creates a result set with 25
documents only.
- But,
cur.count()
will show 200, which is the actual count of
documents by the query's filter.
hasNext()
will return true
.
next()
will return a document.
itcount()
will return 24 (and exhausts the cursor).
itcount()
again will return 0.
cur.count()
will still show 200.