Enumerate traversal pointers Overviewas already mentioned, Db.collection.find () If you do not assign a variable to a VAR declaration, the first 20 records are automatically enumerated.
manually Enumerate pointersIn the MONGO console, assign the query to a VAR-declared variable so that it is not automatically enumerated.
var cur = db.testData.find ();
Then each time this pointer is called, 20 is automatically traversed
cur;
You can also use the next () method of the pointer to get the next record
var cur = db.testData.find ();
while (Cur.hasnext ())
{
Print (Tojson (Cur.next ()));Here the print operation can be replaced with Printjson:Printjson (Cur.next ());
}
You can use the foreach () method of the pointer to traverse the pointer data:
var cur = db.testData.find ();
Cur.foreach (Printjson);
Enumerate subscriptin the MONGO console, you can use the ToArray () method to access the pointer results.
var cur=db.testdata.find ();
var arr = Cur.toarray ();
var item = arr[2];
The ToArray () method loads all the query results into memory, and this method iterates through the pointer.
In addition, some drivers provide a way to directly use the array subscript, which is called the abbreviation for ToArray ().
var cur=db.testdata.find ();
var item = cur[3];
The above two sentences are equivalent to cur.toarray () [3];
MongoDB Operations Manual CRUD query pointers