@@ -10,62 +10,87 @@ cursor.forEach()
10
10
:depth: 1
11
11
:class: singlecol
12
12
13
- Description
14
- -----------
13
+ Definition
14
+ ----------
15
15
16
16
.. method:: cursor.forEach(function)
17
17
18
-
19
18
.. include:: /includes/fact-mongosh-shell-method.rst
20
19
21
-
22
20
Iterates the cursor to apply a JavaScript ``function`` to each
23
21
document from the cursor.
24
22
25
- The :method:`~cursor.forEach()` method has the following prototype
26
- form:
23
+ Syntax
24
+ ------
27
25
28
- .. code-block:: javascript
26
+ The method has the following syntax:
29
27
30
- db.collection.find().forEach(<function>)
28
+ .. code-block:: javascript
29
+ :copyable: false
31
30
32
- The :method:`~cursor.forEach()` method has the following parameter:
31
+ db.collection.find().forEach( <function> )
33
32
33
+ Method Fields
34
+ -------------
34
35
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:
55
37
38
+ .. list-table::
39
+ :header-rows: 1
40
+ :widths: 20 20 80
56
41
57
- Example
58
- -------
42
+ * - Field
43
+ - Type
44
+ - Description
59
45
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.
63
51
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:
65
86
66
- db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } );
87
+ .. code-block:: none
67
88
68
- .. seealso::
89
+ for ( const myDoc of db.users.find() ) {
90
+ print( "User name: " + myDoc.name )
91
+ }
69
92
70
- :method:`cursor.map()` for similar functionality.
93
+ Learn More
94
+ ----------
71
95
96
+ For a method that has similar functionality, see :method:`cursor.map()`.
0 commit comments