Skip to content

Commit 0fc70b8

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-33796-for-of-update (#5353) (#5560)
* DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update * DOCSP-33796-for-of-update --------- Co-authored-by: jason-price-mongodb <[email protected]>
1 parent ac457ef commit 0fc70b8

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

source/reference/method/cursor.forEach.txt

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,87 @@ cursor.forEach()
1010
:depth: 1
1111
:class: singlecol
1212

13-
Description
14-
-----------
13+
Definition
14+
----------
1515

1616
.. method:: cursor.forEach(function)
1717

18-
1918
.. include:: /includes/fact-mongosh-shell-method.rst
2019

21-
2220
Iterates the cursor to apply a JavaScript ``function`` to each
2321
document from the cursor.
2422

25-
The :method:`~cursor.forEach()` method has the following prototype
26-
form:
23+
Syntax
24+
------
2725

28-
.. code-block:: javascript
26+
The method has the following syntax:
2927

30-
db.collection.find().forEach(<function>)
28+
.. code-block:: javascript
29+
:copyable: false
3130

32-
The :method:`~cursor.forEach()` method has the following parameter:
31+
db.collection.find().forEach( <function> )
3332

33+
Method Fields
34+
-------------
3435

35-
.. list-table::
36-
:header-rows: 1
37-
:widths: 20 20 80
38-
39-
* - Parameter
40-
41-
- Type
42-
43-
- Description
44-
45-
* - ``function``
46-
47-
- JavaScript
48-
49-
- A JavaScript function to apply to each document from the cursor. The
50-
``<function>`` signature includes a single argument that is passed the
51-
current document to process.
52-
53-
54-
36+
The method accepts the following field:
5537

38+
.. list-table::
39+
:header-rows: 1
40+
:widths: 20 20 80
5641

57-
Example
58-
-------
42+
* - Field
43+
- Type
44+
- Description
5945

60-
The following example invokes the :method:`~cursor.forEach()` method
61-
on the cursor returned by :method:`~db.collection.find()` to print
62-
the name of each user in the collection:
46+
* - ``function``
47+
- JavaScript code
48+
- Function to apply to each document returned from the cursor. The
49+
function signature includes one field that stores the current
50+
document that is read from the cursor.
6351

64-
.. code-block:: javascript
52+
Examples
53+
--------
54+
55+
Create the ``users`` collection:
56+
57+
.. code-block:: none
58+
59+
db.users.insertMany( [
60+
{ name: "John" },
61+
{ name: "Jane" }
62+
] )
63+
64+
The following example uses ``forEach()`` with the
65+
:method:`~db.collection.find()` method to print the user names that are
66+
read from the ``users`` collection. ``myDoc`` stores the current
67+
document.
68+
69+
.. code-block:: none
70+
71+
db.users.find().forEach( function( myDoc ) {
72+
print( "User name: " + myDoc.name )
73+
} )
74+
75+
Example output:
76+
77+
.. code-block:: none
78+
:copyable: false
79+
80+
User name: John
81+
User name: Jane
82+
83+
Starting in :binary:`~bin.mongosh` 2.1.0, you can also use ``for-of``
84+
loops. The following example returns the same results as the previous
85+
example:
6586

66-
db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } );
87+
.. code-block:: none
6788

68-
.. seealso::
89+
for ( const myDoc of db.users.find() ) {
90+
print( "User name: " + myDoc.name )
91+
}
6992

70-
:method:`cursor.map()` for similar functionality.
93+
Learn More
94+
----------
7195

96+
For a method that has similar functionality, see :method:`cursor.map()`.

0 commit comments

Comments
 (0)