{ "_id" : 1, "scores" : [ { "attempt" : 3, "score" : 7 }, { "attempt" : 2, "score" : 8 }, { "attempt" : 1, "score" : 10 } ] } 

## See also: 

• $push operator, 

• $each modifier, 

• $sort modifier, and 

• $slice modifier. 

## 3.3.7 Iterate a Cursor in the mongo Shell 

The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor. However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results. The following describes ways to manually iterate the cursor to access the documents or to use the iterator index. 

## Manually Iterate the Cursor 

In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var keyword, the cursor does not automatically iterate. 

You can call the cursor variable in the shell to iterate up to 20 times 14 and print the matching documents, as in the following example: 

var myCursor = db.inventory.find( { type: 'food' } ); 

myCursor 

You can also use the cursor method next() to access the documents, as in the following example: 

var myCursor = db.inventory.find( { type: 'food' } ); while (myCursor.hasNext()) { print(tojson(myCursor.next())); } 

As an alternative print operation, consider the printjson() helper method to replace print(tojson()) : 

var myCursor = db.inventory.find( { type: 'food' } ); while (myCursor.hasNext()) { printjson(myCursor.next()); } 

You can use the cursor method forEach() to iterate the cursor and access the documents, as in the following example: 

