@@ -664,9 +664,9 @@ Cursors
664
664
The :method:`find() <db.collection.find()>` method returns a
665
665
:term:`cursor`; however, in the :program:`mongo` shell, if the result
666
666
of the :method:`find() <db.collection.find()>` is not assigned to a
667
- variable, then the cursor is automatically iterated up to 20 times to
668
- print the documents referenced by the cursor, as in the following
669
- example:
667
+ variable, then the cursor is automatically iterated up to 20 times
668
+ [#setShellBatchSize]_ to print the documents referenced by the cursor,
669
+ as in the following example:
670
670
671
671
.. code-block:: javascript
672
672
@@ -676,56 +676,102 @@ This operation will iterate the cursor up to 20 times and print the
676
676
first 20 documents referenced by the cursor.
677
677
678
678
If the result of the :method:`find() <db.collection.find()>` is
679
- assigned to a variable, you can use the cursor method :method:`next()
680
- <cursor.next()>` to access the documents, as in the following example:
679
+ assigned to a variable:
681
680
682
- .. code-block:: javascript
681
+ - you can type the name of the cursor variable to iterate up to 20
682
+ times [#setShellBatchSize]_ and print the documents referenced by the
683
+ cursor, as in the following example:
684
+
685
+ .. code-block:: javascript
683
686
684
- var myCursor = db.inventory.find( { type: 'food' } );
687
+ var myCursor = db.inventory.find( { type: 'food' } );
685
688
686
- var myDocument = myCursor.hasNext() ? myCursor.next() : null;
689
+ myCursor
687
690
688
- if (myDocument) {
689
- var myItem = myDocument.item;
691
+ - you can use the cursor method :method:`next() <cursor.next()>` to
692
+ access the documents, as in the following example:
690
693
691
- print (tojson(myItem));
692
- }
694
+ .. code-block:: javascript
695
+
696
+ var myCursor = db.inventory.find( { type: 'food' } );
697
+
698
+ var myDocument = myCursor.hasNext() ? myCursor.next() : null;
699
+
700
+ if (myDocument) {
701
+
702
+ var myItem = myDocument.item;
703
+
704
+ print(tojson(myItem));
705
+ }
706
+
707
+ To print, you can also use the ``printjson()`` method instead of
708
+ ``print(tojson())``:
709
+
710
+ .. code-block:: javascript
711
+
712
+ if (myDocument) {
713
+
714
+ var myItem = myDocument.item;
715
+
716
+ printjson(myItem);
717
+ }
718
+
719
+ - you can use the cursor method :method:`forEach() <cursor.forEach()>`
720
+ to iterate the cursor and access the documents, as in the following
721
+ example:
722
+
723
+ .. code-block:: javascript
724
+
725
+ var myCursor = db.inventory.find( { type: 'food' } );
726
+
727
+ myCursor.forEach(printjson);
693
728
694
729
See :ref:`JavaScript cursor methods <js-query-cursor-methods>` and your
695
730
:doc:`driver </applications/drivers>` documentation for more
696
731
information on cursor methods.
697
732
698
- Array Mode
699
- ~~~~~~~~~~
733
+ .. [#setShellBatchSize] You can use the ``DBQuery.shellBatchSize`` to
734
+ change the number of iteration from the default value ``20``, as in the
735
+ following example which sets the number to ``10``:
736
+
737
+ .. code-block:: javascript
738
+
739
+ DBQuery.shellBatchSize = 10
740
+
741
+ Iterator Index
742
+ ~~~~~~~~~~~~~~
700
743
701
744
Additionally, with some of the :doc:`drivers </applications/drivers>`,
702
- you can use the cursor in "array mode" and access the documents with an
703
- index (i.e. ``cursor[ index]``) , as in the following example:
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:
704
747
705
748
.. code-block:: javascript
706
749
707
750
var myCursor = db.inventory.find( { type: 'food' } );
708
751
709
- for (var idx = 0; idx < myCursor.length(); idx++ ){
710
- myDoc = myCursor[idx];
711
- print (tojson(myDoc));
712
- }
752
+ myDoc = myCursor[3];
713
753
714
- The "array mode" loads into RAM the documents up to the highest
715
- specified index and should not be used for queries which may return
716
- very large number of data that may exceed memory.
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.
717
757
718
- You can also convert the cursor explicitly to an array, as in the following:
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:
719
761
720
762
.. code-block:: javascript
721
763
722
764
var myCursor = db.inventory.find( { type: 'food' } );
723
765
var documentArray = myCursor.toArray();
724
766
725
- The ``toArray()`` method loads into RAM all documents returned by the cursor.
767
+ The ``toArray()`` method loads into RAM all documents returned by the
768
+ cursor.
726
769
727
770
.. TODO link to toArray() method once the page has been added
728
771
772
+ Cursor Considerations
773
+ ~~~~~~~~~~~~~~~~~~~~~
774
+
729
775
Consider the following behaviors related to cursors:
730
776
731
777
- By default, the server will automatically close the cursor after 10
@@ -758,9 +804,9 @@ Consider the following behaviors related to cursors:
758
804
- Batch size cannot exceed the :ref:`maximum BSON document size
759
805
<limit-bson-document-size>`.
760
806
761
- - As you iterate through the cursor and reach the end of that batch,
762
- if there are more results, :method:`cursor.next()` will perform a
763
- :data:`getmore operation <op>` to retrieve the next batch.
807
+ - As you iterate through the cursor and reach the end of the returned
808
+ batch, if there are more results, :method:`cursor.next()` will
809
+ perform a :data:`getmore operation <op>` to retrieve the next batch.
764
810
765
811
To see how many documents remain in the batch as you iterate the
766
812
cursor, you can use the :method:`cursor.objsLeftInBatch()` method,
0 commit comments