Skip to content

Commit b5c42f9

Browse files
committed
DOCS-853 toArray method
1 parent b588599 commit b5c42f9

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

source/core/read-operations.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,23 +732,25 @@ information on cursor methods.
732732
Iterator Index
733733
~~~~~~~~~~~~~~
734734

735-
In the :program:`mongo` shell, you can use the ``toArray()`` method to
736-
iterate the cursor and return the documents in an array, as in the
737-
following:
735+
In the :program:`mongo` shell, you can use the :method:`toArray()
736+
<cursor.toArray()>` method to iterate the cursor and return the
737+
documents in an array, as in the following:
738738

739739
.. code-block:: javascript
740740

741741
var myCursor = db.inventory.find( { type: 'food' } );
742742
var documentArray = myCursor.toArray();
743743
var myDocument = documentArray[3];
744744

745-
The ``toArray()`` method loads into RAM all documents returned by the
746-
cursor; the ``toArray()`` method exhausts the cursor.
745+
The :method:`toArray() <cursor.toArray()>` method loads into RAM all
746+
documents returned by the cursor; the :method:`toArray()
747+
<cursor.toArray()>` method exhausts the cursor.
747748

748749
Additionally, some :doc:`drivers </applications/drivers>` provide
749750
access to the documents by using an index on the cursor (i.e.
750751
``cursor[index]``). This is a shortcut for first calling the
751-
``toArray()`` method and then using an index on the resulting array.
752+
:method:`toArray() <cursor.toArray()>` method and then using an index
753+
on the resulting array.
752754

753755
Consider the following example:
754756

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
================
2+
cursor.toArray()
3+
================
4+
5+
.. default-domain:: mongodb
6+
7+
.. method:: cursor.toArray()
8+
9+
The :method:`toArray() <cursor.toArray()>` method iterates a cursor
10+
completely to retrieve the documents and return these documents in
11+
an array. The :method:`toArray() <cursor.toArray()>` method loads
12+
into RAM all documents returned by the cursor and exhausts the
13+
cursor.
14+
15+
:returns: An array of documents.
16+
17+
Consider the following example that applies :method:`toArray()
18+
<cursor.toArray()>` to the cursor returned from the :method:`find()
19+
<db.collection.find()>` method:
20+
21+
.. code-block:: javascript
22+
23+
var docArray = db.products.find().toArray();
24+
25+
if (docArray.length > 0) { printjson (docArray[0]); }
26+
27+
The variable ``docArray`` holds the array of documents returned by
28+
:method:`toArray() <cursor.toArray()>`.

0 commit comments

Comments
 (0)