@@ -4,9 +4,9 @@ Using the ``mongo`` Shell
44
55.. default-domain:: mongodb
66
7- :program:`mongo` shell is the MongoDB interactive shell and is part of
7+ The :program:`mongo` shell is the MongoDB interactive shell and is part of
88the `MongoDB distribution <http://www.mongodb.org/downloads>`_. The
9- :program:`mongo` is located in the ``<mongodb installation dir>/bin``
9+ :program:`mongo` program is located in the ``<mongodb installation dir>/bin``
1010directory.
1111
1212The :program:`mongo` shell is a JavaScript shell [#spider-monkey]_ from
@@ -62,9 +62,9 @@ To start the :program:`mongo` shell and connect to your :doc:`MongoDB
6262
6363 use myDatabase
6464
65- See :ref:`mongo-shell-help-db` to list the available databases . See also
66- :ref:`mongo-shell-getSiblingDB` to access a different
67- database from the current database.
65+ To list the available database, use the command ``show dbs`` . See
66+ also :ref:`mongo-shell-getSiblingDB` to access a different database
67+ from the current database.
6868
6969To start the :program:`mongo` shell with other options, see
7070:ref:`examples of starting up mongo <mongo-usage-examples>` and
@@ -103,12 +103,14 @@ From the :program:`mongo` shell, you can use the :doc:`shell methods
103103
104104 If the :program:`mongo` shell does not accept the name of the
105105 collection, for instance if the name contains a space or starts with
106- a number, use an alternate syntax to refer to the collection, as in
107- the following:
106+ a number, you can use an alternate syntax to refer to the
107+ collection, as in the following alternates :
108108
109109 .. code-block:: javascript
110110
111- db['3test'].find()
111+ db["3test"].find()
112+
113+ db.getCollection("3test").find()
112114
113115- The :method:`find() <db.collection.find()>` method is the JavaScript
114116 method to retrieve documents from ``myCollection``. The
@@ -119,9 +121,9 @@ From the :program:`mongo` shell, you can use the :doc:`shell methods
119121 first 20 documents that match the query. The :program:`mongo` shell
120122 will prompt ``Type it`` to iterate another 20 times.
121123
122- You can use the ``DBQuery.shellBatchSize`` to change the number of
123- iteration from the default value ``20``, as in the following example
124- which sets it to ``10``:
124+ You can use the ``DBQuery.shellBatchSize`` variable to change the
125+ number of iteration from the default value ``20``, as in the
126+ following example which sets it to ``10``:
125127
126128 .. code-block:: javascript
127129
@@ -158,8 +160,8 @@ Print
158160
159161The :program:`mongo` shell automatically prints the results of the
160162:method:`find() <db.collection.find()>` method if the returned cursor
161- is not assigned to a variable. To format the result as :term:`JSON` , you can
162- add the ``.pretty()`` to the operation, as in the following:
163+ is not assigned to a variable. To format the result, you can add the
164+ ``.pretty()`` to the operation, as in the following:
163165
164166.. code-block:: javascript
165167
@@ -426,6 +428,22 @@ JavaScript methods to handle the cursor returned from the
426428
427429 db.collection.find().toArray
428430
431+ Some useful methods for handling cursors are:
432+
433+ - :method:`hasNext() <cursor.hasNext()>` which checks whether the
434+ cursor has more documents to return.
435+
436+ - :method:`next() <cursor.next()>` which returns the next document and
437+ moves by one the position of the cursor.
438+
439+ - :method:`forEach(\<function\>) <cursor.forEach()>` which iterates the
440+ whole cursor and applies the ``<function>`` to each document returned
441+ by the cursor.
442+
443+ For examples on iterating a cursor and retrieving the documents from
444+ the cursor, see :ref:`cursor handling <read-operations-cursors>`. See
445+ also :ref:`js-query-cursor-methods` for all available cursor methods.
446+
429447.. _mongo-shell-tips:
430448
431449Tips
@@ -615,7 +633,7 @@ instantiate database connections using the ``Mongo()`` constructor:
615633 new Mongo(<host:port>)
616634
617635Consider the following example that instantiates a new connection to
618- the MongoDB instance running on localhost on the default port and set
636+ the MongoDB instance running on localhost on the default port and sets
619637the current ``db`` to ``myDatabase``:
620638
621639.. code-block:: javascript
0 commit comments