cursor.forEach()

Description

  • cursor.forEach(function)

mongo Shell Method

This page documents the mongo shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.

Iterates the cursor to apply a JavaScript function to eachdocument from the cursor.

The forEach() method has the following prototypeform:

  1. db.collection.find().forEach(<function>)

The forEach() method has the following parameter:

ParameterTypeDescriptionfunctionJavaScriptA JavaScript function to apply to each document from the cursor. The<function> signature includes a single argument that is passed thecurrent document to process.

Example

The following example invokes the forEach() methodon the cursor returned by find() to printthe name of each user in the collection:

  1. db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } );

See also

cursor.map() for similar functionality.