Skip to content

Commit 2ff5702

Browse files
committed
DOCS-675 incorporate scott's first round of feedback
1 parent 8d6d2bf commit 2ff5702

File tree

2 files changed

+141
-41
lines changed

2 files changed

+141
-41
lines changed

source/applications/read.txt

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ Cursor
330330
The :method:`find() <db.collection.find()>` method returns a
331331
:term:`cursor`; however, in the :program:`mongo` shell, if the result
332332
of the :method:`find() <db.collection.find()>` is not assigned to a
333-
variable, then the cursor is automatically iterated up to 20 times to
334-
print the documents referenced by the cursor, as in the following
335-
example:
333+
variable, then the cursor is automatically iterated up to 20 times
334+
[#setShellBatchSize]_ to print the documents referenced by the cursor,
335+
as in the following example:
336336

337337
.. code-block:: javascript
338338

@@ -342,20 +342,55 @@ This operation will iterate the cursor up to 20 times and print the
342342
first 20 documents referenced by the cursor.
343343

344344
If the result of the :method:`find() <db.collection.find()>` is
345-
assigned to a variable, you can use the cursor method :method:`next()
346-
<cursor.next()>` to access the documents, as in the following example:
345+
assigned to a variable:
347346

348-
.. code-block:: javascript
347+
- you can type the name of the cursor variable to iterate up to 20
348+
times [#setShellBatchSize]_ and print the documents referenced by the
349+
cursor, as in the following example:
349350

350-
var myCursor = db.bios.find( { _id: 1 } );
351+
.. code-block:: javascript
351352

352-
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
353+
var myCursor = db.bios.find( { _id: 1 } );
353354

354-
if (myDocument) {
355-
var myName = myDocument.name;
355+
myCursor
356356

357-
print (tojson(myName));
358-
}
357+
- you can use the cursor method :method:`next() <cursor.next()>` to
358+
access the documents, as in the following example:
359+
360+
.. code-block:: javascript
361+
362+
var myCursor = db.bios.find( { _id: 1 } );
363+
364+
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
365+
366+
if (myDocument) {
367+
368+
var myName = myDocument.name;
369+
370+
print (tojson(myName));
371+
}
372+
373+
To print, you can also use the ``printjson()`` method instead of
374+
``print(tojson())``:
375+
376+
.. code-block:: javascript
377+
378+
if (myDocument) {
379+
380+
var myName = myDocument.name;
381+
382+
printjson(myName);
383+
}
384+
385+
- you can use the cursor method :method:`forEach() <cursor.forEach()>`
386+
to iterate the cursor and access the documents, as in the following
387+
example:
388+
389+
.. code-block:: javascript
390+
391+
var myCursor = db.bios.find( { _id: 1 } );
392+
393+
myCursor.forEach(printjson);
359394

360395
For more information on cursor handling, see:
361396

@@ -369,6 +404,13 @@ For more information on cursor handling, see:
369404

370405
- :ref:`JavaScript cursor methods<js-query-cursor-methods>`
371406

407+
.. [#setShellBatchSize] You can use the ``DBQuery.shellBatchSize`` to
408+
change the number of iteration from the default value ``20``, as in the
409+
following example which sets the number to ``10`` :
410+
411+
.. code-block:: javascript
412+
413+
DBQuery.shellBatchSize = 10
372414

373415
Modify Cursor Behavior
374416
``````````````````````
@@ -419,7 +461,8 @@ its behavior, such as:
419461

420462
You may chain these cursor methods; however, the :method:`limit()
421463
<cursor.limit()>` method is always applied after the :method:`sort()
422-
<cursor.sort()>` even if you chain the methods in the reverse order:
464+
<cursor.sort()>` even if you chain the methods in the reverse
465+
order [#dbquery-server]_:
423466

424467
.. code-block:: javascript
425468

@@ -430,6 +473,17 @@ See :ref:`JavaScript cursor methods <js-query-cursor-methods>` and your
430473
information on cursor methods. See :ref:`read-operations-cursors` for
431474
more information regarding cursors.
432475

476+
.. [#dbquery-server] The server treats the query with the sort as a
477+
single object:
478+
479+
.. code-block:: javascript
480+
481+
db.bios.find( { $query: {}, $orderby: { name: 1 } } ).limit( 5 )
482+
483+
For other cursor modifiers that are treated with the query as a
484+
single object, see the :doc:`meta query operators
485+
</reference/meta-query-operators>`.
486+
433487
.. _crud-read-findOne:
434488
.. _crud-read-find-one:
435489

source/core/read-operations.txt

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,9 @@ Cursors
664664
The :method:`find() <db.collection.find()>` method returns a
665665
:term:`cursor`; however, in the :program:`mongo` shell, if the result
666666
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:
670670

671671
.. code-block:: javascript
672672

@@ -676,56 +676,102 @@ This operation will iterate the cursor up to 20 times and print the
676676
first 20 documents referenced by the cursor.
677677

678678
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:
681680

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
683686

684-
var myCursor = db.inventory.find( { type: 'food' } );
687+
var myCursor = db.inventory.find( { type: 'food' } );
685688

686-
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
689+
myCursor
687690

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:
690693

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);
693728

694729
See :ref:`JavaScript cursor methods <js-query-cursor-methods>` and your
695730
:doc:`driver </applications/drivers>` documentation for more
696731
information on cursor methods.
697732

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+
~~~~~~~~~~~~~~
700743

701744
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:
704747

705748
.. code-block:: javascript
706749

707750
var myCursor = db.inventory.find( { type: 'food' } );
708751

709-
for (var idx = 0; idx < myCursor.length(); idx++ ){
710-
myDoc = myCursor[idx];
711-
print (tojson(myDoc));
712-
}
752+
myDoc = myCursor[3];
713753

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.
717757

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:
719761

720762
.. code-block:: javascript
721763

722764
var myCursor = db.inventory.find( { type: 'food' } );
723765
var documentArray = myCursor.toArray();
724766

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.
726769

727770
.. TODO link to toArray() method once the page has been added
728771

772+
Cursor Considerations
773+
~~~~~~~~~~~~~~~~~~~~~
774+
729775
Consider the following behaviors related to cursors:
730776

731777
- By default, the server will automatically close the cursor after 10
@@ -758,9 +804,9 @@ Consider the following behaviors related to cursors:
758804
- Batch size cannot exceed the :ref:`maximum BSON document size
759805
<limit-bson-document-size>`.
760806

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.
764810

765811
To see how many documents remain in the batch as you iterate the
766812
cursor, you can use the :method:`cursor.objsLeftInBatch()` method,

0 commit comments

Comments
 (0)