@@ -5,9 +5,10 @@ Getting Started with the MongoDB JavaScript Shell
55.. default-domain:: mongodb
66
77This tutorial is an introduction to creating and retrieving database
8- documents using the MongoDB JavaScript Shell.
8+ documents using the MongoDB JavaScript Shell, also referred to as the
9+ :program:`mongo` shell.
910
10- The MongoDB JavaScript shell, also referred to as the  :program:`mongo`
11+ The :program:`mongo`
1112shell, is a full JavaScript shell that gives you access to all create,
1213read, update, and delete operations in a MongoDB database. The shell
1314also allows you to use all JavaScript functions and syntax. See the
@@ -24,8 +25,15 @@ installing MongoDB and starting the database server, see
2425   :local:
2526   :depth: 3
2627
27- Connect to the :program:`mongod`
28- --------------------------------
28+ Connect to a Database
29+ ---------------------
30+ 
31+ In this section you connect to the database server, referred to as the
32+ :program:`mongod` (pronounced "Mongo D"), and then you connect to a
33+ database. This section also tells you how to access help.
34+ 
35+ Connect to a :program:`mongod`
36+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2937
3038From the command line, start the MongoDB JavaScript shell by issuing the
3139:program:`mongo` command:
@@ -35,7 +43,7 @@ From the command line, start the MongoDB JavaScript shell by issuing the
3543   mongo
3644
3745By default, :program:`mongo` looks for a database server listening on
38- port ``27017``. To look for  a server listening  on a different port, use
46+ port ``27017``. To connect to  a server on a different port, use
3947the :option:`--port <mongod --port>` option.
4048
4149Select a Database
@@ -54,9 +62,8 @@ databases by issuing the following command:
5462
5563      use mydb
5664
57- #. Confirm that the ``mydb`` database is the context for your session by
58-    typing the following command, which returns the name of the database
59-    you are on:
65+ #. Confirm that the ``mydb`` database is selected by
66+    typing the following command, which returns the name of the current database:
6067
6168   .. code-block:: javascript
6269
@@ -70,7 +77,8 @@ databases by issuing the following command:
7077Display :program:`mongod` Help
7178~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7279
73- To return a list of help pages for the :program:`mongo` shell, issue the
80+ As you go through this tutorial and work with the :program:`mongo`
81+ shell, you can access help from the shell at any time by issuing the
7482following command:
7583
7684.. code-block:: javascript
@@ -107,25 +115,24 @@ Insert Individual Documents
107115
108116      use mydb
109117
110- #. Create two documents, named ``j`` and ``t ``, by issuing the following
118+ #. Create two documents, named ``j`` and ``k ``, by issuing the following
111119   sequence of operations:
112120
113121   .. code-block:: javascript
114122
115123      j = { name : "mongo" }
116-       t  = { x : 3 }
124+       k  = { x : 3 }
117125
118- #. Insert the ``j`` and ``t `` documents as documents in  the collection
126+ #. Insert the ``j`` and ``k `` documents into  the collection
119127   ``things`` by entering the following sequence of operations:
120128
121129   .. code-block:: javascript
122130
123-       db.things.insert(j )
124-       db.things.insert(t )
131+       db.things.insert( j  )
132+       db.things.insert( k  )
125133
126-    Once you insert the first document (in this case, the ``j``
127-    document), MongoDB creates both the ``mydb`` database and the ``things``
128-    collection.
134+    Once you insert the first document, MongoDB creates both the ``mydb``
135+    database and the ``things`` collection.
129136
130137#. Confirm that the collection named ``things`` has been created by issuing
131138   the following command:
@@ -157,27 +164,25 @@ Insert Individual Documents
157164   field, so :program:`mongo` creates a unique :doc:`ObjectId
158165   </object-id>` value for the field.
159166
160-    Unlike the documents in the ``things`` collection, most MongoDB
161-    collections store documents with the same structure.
162- 
163167Insert Multiple Documents Using a For Loop
164168~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165169
166- 1. From the :program:`mongo` shell, add more documents to the collection 
167-    named ``things``  by issuing the following ``for`` loop:
170+ 1. From the :program:`mongo` shell, add more documents to the ``things`` 
171+    collection  by issuing the following ``for`` loop:
168172
169173   .. code-block:: javascript
170174
171-       for (var i = 1; i <= 20; i++) db.things.insert({ x : 4, j : i} )
175+       for (var i = 1; i <= 20; i++) db.things.insert( {  x : 4  , j : i }  )
172176
173177#. Query the collection by issuing the following command:
174178
175179   .. code-block:: javascript
176180
177181      db.things.find()
178182
179-    MongoDB returns the first 20 documents in the collection. Your
180-    :doc:`ObjectId </object-id>` values will be different:
183+    The :program:`mongo` shell displays the first 20 documents in the
184+    collection. Your :doc:`ObjectId </object-id>` values will be
185+    different:
181186
182187   .. code-block:: javascript
183188
@@ -201,9 +206,8 @@ Insert Multiple Documents Using a For Loop
201206      { "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
202207      { "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
203208      { "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
204-       has more
205209
206- #. Type ``it`` (for iterate)  to return the next batch of results .
210+ #. Type ``it`` to display more documents from the collection .
207211
208212   MongoDB returns the following:
209213
@@ -212,57 +216,39 @@ Insert Multiple Documents Using a For Loop
212216      { "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
213217      { "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
214218
215-    When you issue the :method:`find() <db.collection.find()>` method,
216-    MongoDB creates a "cursor" object that contains all the documents
217-    returned by the query. The shell iterates over the cursor and
218-    returns the first 20 documents. To continue to iterate over
219-    the cursor, you type ``it``.
220- 
221-    The next procedure describes additional ways to work with the cursor
222-    object.
223- 
224219For more information on inserting new documents, see :ref:`crud-create-insert`.
225220
226- Retrieve Data from a Collection
227- -------------------------------
228- 
229- You have already returned documents from a collection by using the
230- :method:`find() <db.collection.find()>` method in its simplest form.
231- 
232- In these procedures you will determine what is returned and displayed by
233- working with:
234- 
235- - The cursor object created by :method:`find() <db.collection.find()>`
236- 
237- - :ref:`Query documents <read-operations-query-document>`
238- 
239- - The :method:`limit() <cursor.limit()>` method
240- 
241- Working with the Cursor Object
242- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221+ Working with the Cursor
222+ -----------------------
243223
244- When you query a :term:`collection`, MongoDB returns a "cursor" object that
245- contains the results of the query. Cursor objects give you flexibility
246- in how to work with results.
224+ When you query a :term:`collection`, MongoDB returns a "cursor" object
225+ that contains the results of the query. The :program:`mongo` shell then
226+ iterates over the cursor to display the results. Instead of displaying
227+ all the results at once, which could number into the thousands or more,
228+ depending on your database, the shell iterates over the cursor 20 times
229+ to display the first 20 results.
247230
248- The previous procedure showed how to display results by iterating over a
249- cursor using the ``it`` command. This procedure shows other ways to work
250- with a cursor.
231+ As you saw in the previous procedure, you can display more results using
232+ the ``it`` command. The ``it`` command tells the shell to iterate 20
233+ more times over the cursor and display the next 20 results. In the
234+ previous procedure, the cursor contained only two more documents, and so
235+ only two more documents displayed.
251236
252- For documentation on the cursor object, see :ref:`crud-read-cursor`.
237+ The procedures in this section show other ways to work with a cursor.
238+ For comprehensive documentation on cursors, see :ref:`crud-read-cursor`.
253239
254- Return More than 20 Documents 
255- `````````````````````````````
240+ Iterate over the Cursor with a Loop 
241+ ``````````````````````````````````` 
256242
257- 1. In the MongoDB JavaScript shell, query the collection named  ``things``
243+ 1. In the MongoDB JavaScript shell, query the ``things`` collection 
258244   and assign the resulting cursor object to the ``c`` variable:
259245
260246   .. code-block:: javascript
261247
262248      var c = db.things.find()
263249
264- #. To return  the full result set without limiting the results  to 20 
265-    documents, use a ``while`` loop to iterate over  the ``c`` variable:
250+ #. Print  the full result set by using a ``while`` loop  to iterate over 
251+    the ``c`` variable:
266252
267253   .. code-block:: javascript
268254
@@ -303,16 +289,16 @@ Return More than 20 Documents
303289Use Array Operations with the Cursor
304290````````````````````````````````````
305291
306- You can treat a cursor like an array. You also can convert a cursor to an array. 
292+ You can treat a cursor like an array.
307293
308- 1. In the MongoDB JavaScript shell, query the collection named  ``things``
294+ 1. In the MongoDB JavaScript shell, query the ``things`` collection 
309295   and assign the resulting cursor object to the ``c`` variable:
310296
311297   .. code-block:: javascript
312298
313299      var c = db.things.find()
314300
315- #. To find the document at the fifth position (i.e. at subscript  ``4``),
301+ #. To find the document at the fifth position (i.e. at array index  ``4``),
316302   issue the following command:
317303
318304   .. code-block:: javascript
@@ -325,46 +311,23 @@ You can treat a cursor like an array. You also can convert a cursor to an array.
325311
326312      { "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
327313
328-    When you query based on a document's position in the cursor object,
329-    :program:`mongo` loads into RAM all the documents prior to the
330-    highest position queried. For example, if you query the document at
331-    ``c[4]``, :program:`mongo` loads into RAM the documents at ``c[0]``,
332-    ``c[1]``, ``c[2]`` and ``c[3]`` as well. For very large result sets,
333-    you can run out of memory. For queries that return large result sets,
334-    it is better to iterate over the cursor using any of the iteration
335-    approaches already described.
336- 
337- #. To convert the cursor to an array, use ``toArray()``. Issue the
338-    following operations to create an array ``a`` and to find the
339-    document at the sixth position:
340- 
341-    .. code-block:: javascript
342- 
343-       var a = db.things.find().toArray()
344-       a [ 5 ]
345- 
346-    MongoDB returns the following:
347- 
348-    .. code-block:: javascript
314+    When you access a document based on its indexed position in the cursor object,
315+    :program:`mongo` also loads into RAM all documents with lower index values.
349316
350-       { "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 } 
351- 
352- For more information on the cursor object, see :ref:`crud-read-cursor` .
317+    For example, if you access the document at ``c[4]``, :program:`mongo` 
318+    loads into RAM the documents at ``c[0]`` through ``c[3]`` as well. 
319+    For very large result sets, you can run out of memory .
353320
354- .. todo Add:
355-    MongoDB cursors are not snapshots. Use explicit locking to perform
356-    a snapshotted query.
357-    And then link to kay's isolated operations document
321+ For more information on the cursor, see :ref:`crud-read-cursor`.
358322
359323Query for Specific Documents
360324~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361325
362326In this procedure, you query for specific documents in the ``things``
363327:term:`collection` by passing a "query document" as a parameter to the
364328:method:`find() <db.collection.find()>` method. A query document
365- specifies the field/value pair the query must match to return a
366- document. For more information, see
367- :ref:`read-operations-query-document`.
329+ specifies the criteria the query must match to return a document. For
330+ more information, see :ref:`read-operations-query-document`.
368331
369332To query for specific documents, do the following:
370333
@@ -377,7 +340,7 @@ To query for specific documents, do the following:
377340
378341      db.things.find( { name : "mongo" } )
379342
380-    MongoDB returns  the one document that fits this criteria. Your
343+    MongoDB displays  the one document that fits this criteria. Your
381344   :doc:`ObjectId </object-id>` value will be different:
382345
383346   .. code-block:: javascript
@@ -419,17 +382,17 @@ To query for specific documents, do the following:
419382      { "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
420383
421384#. Query for all documents where ``x`` has a value of ``4``, as in the
422-    last step, but this time return only the value of ``j``. To do this,
423-    you add the ``{ j : true }`` document as a second parameter to
424-    :method:`find() <db.collection.find()>`. The document lists the
425-    fields to be returned. Issue the following command:
385+    last step, but this time return only the value of ``j`` (and the
386+    ``_id`` field, which is returned by default). To do this, you add the
387+    ``{ j : true }`` document as a second parameter to :method:`find()
388+    <db.collection.find()>`. The document lists the fields to be
389+    returned. Issue the following command:
426390
427391   .. code-block:: javascript
428392
429393      db.things.find( { x : 4 } , { j : true } )
430394
431-    MongoDB returns the following results, excluding the ``x`` field. The
432-    ``_id`` field is included by default:
395+    MongoDB returns the following results:
433396
434397   .. code-block:: javascript
435398
@@ -495,6 +458,8 @@ be different:
495458   { "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
496459   { "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
497460
461+ .. todo Add sections on Update and Remove
462+ 
498463Continuing to Use MongoDB
499464-------------------------
500465
0 commit comments