Skip to content

Toarray 2 #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions source/core/read-operations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -732,23 +732,25 @@ 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()
<cursor.toArray()>` method to iterate the cursor and return the
documents in an array, as in the following:

.. code-block:: javascript

var myCursor = db.inventory.find( { type: 'food' } );
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() <cursor.toArray()>` method loads into RAM all
documents returned by the cursor; the :method:`toArray()
<cursor.toArray()>` method exhausts the cursor.

Additionally, some :doc:`drivers </applications/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() <cursor.toArray()>` method and then using an index
on the resulting array.

Consider the following example:

Expand Down
26 changes: 26 additions & 0 deletions source/reference/method/cursor.toArray.txt
Original file line number Diff line number Diff line change
@@ -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()`.