diff --git a/source/core/read-operations.txt b/source/core/read-operations.txt index 563f7f0ee6b..5e893206916 100644 --- a/source/core/read-operations.txt +++ b/source/core/read-operations.txt @@ -732,9 +732,9 @@ information on cursor methods. Iterator Index ~~~~~~~~~~~~~~ -In the :program:`mongo` shell, you can use the ``toArray()`` method to -iterate the cursor and return the documents in an array, as in the -following: +In the :program:`mongo` shell, you can use the :method:`toArray() +` method to iterate the cursor and return the +documents in an array, as in the following: .. code-block:: javascript @@ -742,13 +742,15 @@ following: var documentArray = myCursor.toArray(); var myDocument = documentArray[3]; -The ``toArray()`` method loads into RAM all documents returned by the -cursor; the ``toArray()`` method exhausts the cursor. +The :method:`toArray() ` method loads into RAM all +documents returned by the cursor; the :method:`toArray() +` method exhausts the cursor. Additionally, some :doc:`drivers ` provide access to the documents by using an index on the cursor (i.e. ``cursor[index]``). This is a shortcut for first calling the -``toArray()`` method and then using an index on the resulting array. +:method:`toArray() ` method and then using an index +on the resulting array. Consider the following example: diff --git a/source/reference/method/cursor.toArray.txt b/source/reference/method/cursor.toArray.txt new file mode 100644 index 00000000000..ba08a45b09c --- /dev/null +++ b/source/reference/method/cursor.toArray.txt @@ -0,0 +1,26 @@ +================ +cursor.toArray() +================ + +.. default-domain:: mongodb + +.. method:: cursor.toArray() + + The :method:`~cursor.toArray()` method returns an array that + contains all the documents from a cursor. The method iterates + completely the cursor, loading all the documents into RAM and + exhausting the cursor. + + :returns: An array of documents. + +Consider the following example that applies :method:`~cursor.toArray()` +to the cursor returned from the :method:`~db.collection.find()` method: + +.. code-block:: javascript + + var allProductsArray = db.products.find().toArray(); + + if (allProductsArray.length > 0) { printjson (allProductsArray[0]); } + +The variable ``allProductsArray`` holds the array of documents returned by +:method:`~cursor.toArray()`.