From d4d579140ff038e03c49e3a8610506e2ed936932 Mon Sep 17 00:00:00 2001 From: kay Date: Tue, 8 Jan 2013 19:05:04 -0500 Subject: [PATCH 1/2] DOCS-918 forEach javascript function information --- source/reference/method/cursor.forEach.txt | 50 +++++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/source/reference/method/cursor.forEach.txt b/source/reference/method/cursor.forEach.txt index 5d57dbab266..d5cb97f14fe 100644 --- a/source/reference/method/cursor.forEach.txt +++ b/source/reference/method/cursor.forEach.txt @@ -6,15 +6,53 @@ cursor.forEach() .. method:: cursor.forEach(function) - :param function: function to apply to each document visited by the cursor. + The :method:`~cursor.forEach()` method iterates the cursor + to apply function to each document from the cursor. - Provides the ability to loop or iterate over the cursor returned by - a :method:`db.collection.find()` query and returns each result on the - shell. Specify a JavaScript function as the argument for the - :method:`cursor.forEach()` function. Consider the following example: + The :method:`~cursor.forEach()` method accepts the following + argument: + + :param function: + + JavaScript function to apply to each document from the + cursor. The function signature includes a single argument + which is passed the current document to process, as in the + following prototype: + + .. code-block:: javascript + + function(doc) { + ... + } + + However, if the signature is missing the argument, you can + access the document using the reserved + ``arguments`` [#arguments]_ variable within the function, + specifically ``arguments[0]``, as in the following prototype: + + .. code-block:: javascript + + function() { + doc = arguments[0]; + ... + } + + .. [#arguments] The ``arguments`` variable is an array + and thus, you can access ``arguments.length`` attribute to + determine the number of arguments. + + Consider the following example which appends the + :method:`~cursor.forEach()` method to a + :method:`~db.collection.find()` query to iterate through the cursor + returned from the :method:`~db.collection.find()` and apply the + function to each document: .. code-block:: javascript - db.users.find().forEach( function(u) { print("user: " + u.name); } ); + db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } ); + + The example finds all documents in the ``users`` collection and + applies function to each document in order to print the name from each + document. .. seealso:: :method:`cursor.map()` for similar functionality. From d4ef057287f9bb2679255f60498849519cf82703 Mon Sep 17 00:00:00 2001 From: kay Date: Wed, 9 Jan 2013 18:56:20 -0500 Subject: [PATCH 2/2] DOCS-918 incorporate jeremy's feedback --- source/reference/method/cursor.forEach.txt | 24 ++++++++-------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/source/reference/method/cursor.forEach.txt b/source/reference/method/cursor.forEach.txt index d5cb97f14fe..8d64368c61b 100644 --- a/source/reference/method/cursor.forEach.txt +++ b/source/reference/method/cursor.forEach.txt @@ -4,19 +4,19 @@ cursor.forEach() .. default-domain:: mongodb -.. method:: cursor.forEach(function) +.. method:: cursor.forEach() - The :method:`~cursor.forEach()` method iterates the cursor - to apply function to each document from the cursor. + The :method:`~cursor.forEach()` method iterates the cursor to apply + a JavaScript ```` to each document from the cursor. The :method:`~cursor.forEach()` method accepts the following argument: - :param function: + :param : JavaScript function to apply to each document from the - cursor. The function signature includes a single argument - which is passed the current document to process, as in the + cursor. The ```` signature includes a single argument + that is passed the current document to process, as in the following prototype: .. code-block:: javascript @@ -41,18 +41,12 @@ cursor.forEach() and thus, you can access ``arguments.length`` attribute to determine the number of arguments. - Consider the following example which appends the - :method:`~cursor.forEach()` method to a - :method:`~db.collection.find()` query to iterate through the cursor - returned from the :method:`~db.collection.find()` and apply the - function to each document: + The following example invokes the :method:`~cursor.forEach()` method + on the cursor returned by :method:`~db.collection.find()` to print + the name of each user in the collection: .. code-block:: javascript db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } ); - The example finds all documents in the ``users`` collection and - applies function to each document in order to print the name from each - document. - .. seealso:: :method:`cursor.map()` for similar functionality.