MongoDB (15)-the cursor in the query operation

The cursor in the db.collection.find() method

  • The method will return a cursor at the end
  • Under normal circumstances, the cursor must be iterated to access the document

Important matters

  • When the find() function is called, the Shell does not query the database immediately, but only sends the query request when it actually starts to get the results
  • Almost every method of the cursor object will return the cursor object itself, which can facilitate the call of chained functions
  • Using cursors to output documents in MongoDB Shell includes two situations. If the cursor returned by the find() function is not assigned to a local variable for storage, the cursor will automatically iterate 20 times by default. If you assign the cursor returned by the find() function to a local variable, you can use the function provided by the cursor object to perform manual iteration.
  • Use the cleared cursor to perform iterative output, the displayed content is empty
  • The existence of the entire process from creation to destruction of a cursor is called the life cycle of the cursor, which includes three stages of creation, use and destruction of the cursor. When the client uses the find() function to initiate a query request to the server, a cursor is created on the server, and then the cursor function can be used to manipulate the query results

Iterative loop cursor

Directly call variables

var myCursor = db.users.find( { type: 2 } ); myCursor

Use the next() method of cursor

var myCursor = db.users.find( { type: 2 } ); while (myCursor.hasNext()) {   print(tojson(;}

Use printjson() instead of print() method

var myCursor = db.users.find( { type: 2 } ); while (myCursor.hasNext()) {   printjson(;}

Use cursor's forEach() method

var myCursor =  db.users.find( { type: 2 } ); myCursor.forEach(printjson);

Use pretty() to print directly

Isn't it fragrant? Why use cursors to write code? I don’t understand.

> db.inventory.find({item : /^p/}).pretty()

Common cursor method names

Method nameeffect
hasNextDetermine if there are more documents
nextUsed to get the next document
toArrayPut the query structure into the array
countThe result of the query is the total number of documents
limitLimit the number of query results returned
skipSkip the specified number of documents
sortSort query results
objsLeftlnBatchView the number of documents remaining in the current batch that have not been iterated
addOptionSet auxiliary options for the cursor, modify the default behavior of the cursor
hintMandatory use of the specified index for the query
explainUsed to obtain the query execution process report
snapshotUse snapshots for query results

(The commonly used cursor methods will be explained in detail later)

The following three situations will cause the cursor to be destroyed

  • The cursor variable saved by the client is not in scope.
  • After the cursor traversal is completed, or the client actively sends a termination message
  • No operation on the cursor within 10 minutes on the server side