- Reference >
mongo
Shell Methods >- Cursor Methods >
- cursor.forEach()
cursor.forEach()¶
On this page
Description¶
-
cursor.
forEach
(function)¶ mongo
Shell MethodThis page documents the
mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.Iterates the cursor to apply a JavaScript
function
to each document from the cursor.The
forEach()
method has the following prototype form:The
forEach()
method has the following parameter:Parameter Type Description function
JavaScript A JavaScript function to apply to each document from the cursor. The <function>
signature includes a single argument that is passed the current document to process.
Example¶
The following example invokes the forEach()
method
on the cursor returned by find()
to print
the name of each user in the collection:
See also
cursor.map()
for similar functionality.