@@ -741,31 +741,36 @@ information on cursor methods.
741
741
Iterator Index
742
742
~~~~~~~~~~~~~~
743
743
744
- Additionally, with some of the :doc:`drivers </applications/drivers>`,
745
- you can iterate with an index (i.e. ``cursor[index]``) and return the
746
- document at the index position, as in the following example :
744
+ In the :program:`mongo` shell, you can use the ``toArray()`` method to
745
+ iterate the cursor and return the documents in an array, as in the
746
+ following:
747
747
748
748
.. code-block:: javascript
749
749
750
750
var myCursor = db.inventory.find( { type: 'food' } );
751
+ var documentArray = myCursor.toArray();
752
+ var myDocument = documentArray[3];
751
753
752
- myDoc = myCursor[3];
754
+ The ``toArray()`` method loads into RAM all documents returned by the
755
+ cursor; the ``toArray()`` method exhausts the cursor.
753
756
754
- Accessing the document with an index iterates the cursor and loads into
755
- RAM the documents up to the index and should not be used for queries
756
- which may return very large number of data that may exceed memory.
757
+ Additionally, some :doc:`drivers </applications/drivers>` provide
758
+ access to the documents by using an index on the cursor (i.e.
759
+ ``cursor[index]``). This is a shortcut for first calling the
760
+ ``toArray()`` method and then using an index on the resulting array.
757
761
758
- In the :program:`mongo` shell, you can use the ``toArray()`` method to
759
- iterate the cursor and return the documents in an array, as in the
760
- following:
762
+ Consider the following example:
761
763
762
764
.. code-block:: javascript
763
765
764
766
var myCursor = db.inventory.find( { type: 'food' } );
765
- var documentArray = myCursor.toArray() ;
767
+ var myDocument = myCursor[3] ;
766
768
767
- The ``toArray()`` method loads into RAM all documents returned by the
768
- cursor.
769
+ The ``myCursor[3]`` is equivalent to the following example:
770
+
771
+ .. code-block:: javascript
772
+
773
+ myCursor.toArray() [3];
769
774
770
775
.. TODO link to toArray() method once the page has been added
771
776
@@ -777,31 +782,29 @@ Consider the following behaviors related to cursors:
777
782
- By default, the server will automatically close the cursor after 10
778
783
minutes of inactivity or if the cursor has been exhausted. To
779
784
override this behavior, you can specify the "noTimeout" :wiki:`wire
780
- protocol option <Mongo Wire Protocol>` in your query; however, you
785
+ protocol flag <Mongo Wire Protocol>` in your query; however, you
781
786
should either close the cursor manually or exhaust the cursor. In the
782
- :program:`mongo` shell, you can set the "noTimeout" option . See your
787
+ :program:`mongo` shell, you can set the "noTimeout" flag . See your
783
788
:doc:`driver </applications/drivers>` documentation for information
784
789
on setting the "noTimeout" option.
785
790
786
- - Write operations may interleave during latent access to the cursor.
787
- This may cause a document to be returned multiple times. To handle
788
- this situation, see the information on :wiki:`snapshot mode <How to do
789
- Snapshotted Queries in the Mongo Database>`.
790
-
791
- .. TODO link instead to faq snapshot when that becomes available.
791
+ - Because the cursor is not isolated during its lifetime, intervening
792
+ write operations may cause a document to be returned multiple times.
793
+ To handle this situation, see the information on :wiki:`snapshot mode
794
+ <How to do Snapshotted Queries in the Mongo Database>`.
792
795
793
796
- The MongoDB server returns the query results in batches:
794
797
795
- - For most queries, the default size of each batch is either 101
796
- documents or enough documents to exceed 1 megabyte, whichever
797
- condition is met first . To override the default size of each batch,
798
- see :method:`cursor.batchSize()` and :method:`cursor.limit()`.
798
+ - For most queries, the *first* batch returns 101 documents or just
799
+ enough documents to exceed 1 megabyte. Subsequent batch size is 4
800
+ megabytes . To override the default size of the batch, see
801
+ :method:`cursor.batchSize()` and :method:`cursor.limit()`.
799
802
800
803
- For queries that include a sort operation *without* an index, the
801
804
server must load all the documents in memory to perform the sort
802
805
and will return all documents in the first batch.
803
806
804
- - Batch size cannot exceed the :ref:`maximum BSON document size
807
+ - Batch size will not exceed the :ref:`maximum BSON document size
805
808
<limit-bson-document-size>`.
806
809
807
810
- As you iterate through the cursor and reach the end of the returned
@@ -820,25 +823,27 @@ Consider the following behaviors related to cursors:
820
823
821
824
myCursor.objsLeftInBatch();
822
825
823
- - Batch results may provide performance benefits if you only need to
824
- access a subset of the results, as in the following example which only
825
- requires the first two documents:
826
+ - You can use the command :dbcommand:`cursorInfo` to retrieve the
827
+ following information on cursors:
826
828
827
- .. code-block:: javascript
829
+ - total number of open cursors
828
830
829
- var myCursor = db.inventory.find();
831
+ - size of the client cursors in current use
830
832
831
- var myFirstDocument = myCursor.hasNext() ? myCursor.next() : null;
832
- var mySecondDocument = myCursor.hasNext() ? myCursor.next() : null;
833
+ - number of timed out cursors since the last server restart
833
834
834
- - You can retrieve cursor information, such as the number of open
835
- cursors, using the command :dbcommand:`cursorInfo`, as in the
836
- following example:
835
+ Consider the following example:
837
836
838
837
.. code-block:: javascript
839
838
840
839
db.runCommand( { cursorInfo: 1 } )
841
840
841
+ The result from the command returns the following documentation:
842
+
843
+ .. code-block:: javascript
844
+
845
+ { "totalOpen" : <number>, "clientCursors_size" : <number>, "timedOut" : <number>, "ok" : 1 }
846
+
842
847
.. _read-operations-aggregation:
843
848
844
849
Aggregation
0 commit comments