- Reference >
mongoshMethods >- Cursor Methods >
- cursor.forEach()
cursor.forEach()¶
On this page
Description¶
-
cursor.forEach(function)¶ Important
mongoshMethodThis page documents a
mongoshmethod. This is not the documentation for a language-specific driver such as Node.js.For MongoDB API drivers, refer to the language-specific :driver:`MongoDB driver documentation </>`.
For the legacy
mongoshell documentation, refer to the documentation for the corresponding MongoDB Server release:Iterates the cursor to apply a JavaScript
functionto each document from the cursor.The
forEach()method has the following prototype form:The
forEach()method has the following parameter:Parameter Type Description functionJavaScript 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.