In mongo shell when find() query executed it return cursor with all document in the collection. However, if the returned cursor is not assigned to any variable then first 20 documents iterated automatically.
In other word when a query run to mongoDB whatever it returns is cursor.
Create a cursor in mongo shell
var cursorVar = db.collection.find();
Cursor Iteration
The cursor.next() function used to retrieve the next batch and display it to shell. For confirmation that cursor has next element cursor.hasNext() function used. Following example used cursor to get data and iterate through loop. Run these series of command into mongo shell.
var cursor = db.accounts.find();
while(cursor.hasNext()) printjson(cursor.next());
By default, the server will close the cursor after if found the inactivity of 10 minutes, or if client has exhausted the cursor this process is done automatically by the server.
0 Comment(s)