@@ -5,9 +5,10 @@ Getting Started with the MongoDB JavaScript Shell
5
5
.. default-domain:: mongodb
6
6
7
7
This 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.
9
10
10
- The MongoDB JavaScript shell, also referred to as the :program:`mongo`
11
+ The :program:`mongo`
11
12
shell, is a full JavaScript shell that gives you access to all create,
12
13
read, update, and delete operations in a MongoDB database. The shell
13
14
also allows you to use all JavaScript functions and syntax. See the
@@ -24,8 +25,15 @@ installing MongoDB and starting the database server, see
24
25
:local:
25
26
:depth: 3
26
27
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29
37
30
38
From the command line, start the MongoDB JavaScript shell by issuing the
31
39
:program:`mongo` command:
@@ -35,7 +43,7 @@ From the command line, start the MongoDB JavaScript shell by issuing the
35
43
mongo
36
44
37
45
By 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
39
47
the :option:`--port <mongod --port>` option.
40
48
41
49
Select a Database
@@ -54,9 +62,8 @@ databases by issuing the following command:
54
62
55
63
use mydb
56
64
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:
60
67
61
68
.. code-block:: javascript
62
69
@@ -70,7 +77,8 @@ databases by issuing the following command:
70
77
Display :program:`mongod` Help
71
78
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
79
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
74
82
following command:
75
83
76
84
.. code-block:: javascript
@@ -107,25 +115,24 @@ Insert Individual Documents
107
115
108
116
use mydb
109
117
110
- #. Create two documents, named ``j`` and ``t ``, by issuing the following
118
+ #. Create two documents, named ``j`` and ``k ``, by issuing the following
111
119
sequence of operations:
112
120
113
121
.. code-block:: javascript
114
122
115
123
j = { name : "mongo" }
116
- t = { x : 3 }
124
+ k = { x : 3 }
117
125
118
- #. Insert the ``j`` and ``t `` documents as documents in the collection
126
+ #. Insert the ``j`` and ``k `` documents into the collection
119
127
``things`` by entering the following sequence of operations:
120
128
121
129
.. code-block:: javascript
122
130
123
- db.things.insert(j )
124
- db.things.insert(t )
131
+ db.things.insert( j )
132
+ db.things.insert( k )
125
133
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.
129
136
130
137
#. Confirm that the collection named ``things`` has been created by issuing
131
138
the following command:
@@ -157,27 +164,25 @@ Insert Individual Documents
157
164
field, so :program:`mongo` creates a unique :doc:`ObjectId
158
165
</object-id>` value for the field.
159
166
160
- Unlike the documents in the ``things`` collection, most MongoDB
161
- collections store documents with the same structure.
162
-
163
167
Insert Multiple Documents Using a For Loop
164
168
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165
169
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:
168
172
169
173
.. code-block:: javascript
170
174
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 } )
172
176
173
177
#. Query the collection by issuing the following command:
174
178
175
179
.. code-block:: javascript
176
180
177
181
db.things.find()
178
182
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:
181
186
182
187
.. code-block:: javascript
183
188
@@ -201,9 +206,8 @@ Insert Multiple Documents Using a For Loop
201
206
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
202
207
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
203
208
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
204
- has more
205
209
206
- #. Type ``it`` (for iterate) to return the next batch of results .
210
+ #. Type ``it`` to display more documents from the collection .
207
211
208
212
MongoDB returns the following:
209
213
@@ -212,57 +216,39 @@ Insert Multiple Documents Using a For Loop
212
216
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
213
217
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
214
218
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
-
224
219
For more information on inserting new documents, see :ref:`crud-create-insert`.
225
220
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
+ -----------------------
243
223
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.
247
230
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.
251
236
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`.
253
239
254
- Return More than 20 Documents
255
- `````````````````````````````
240
+ Iterate over the Cursor with a Loop
241
+ ```````````````````````````````````
256
242
257
- 1. In the MongoDB JavaScript shell, query the collection named ``things``
243
+ 1. In the MongoDB JavaScript shell, query the ``things`` collection
258
244
and assign the resulting cursor object to the ``c`` variable:
259
245
260
246
.. code-block:: javascript
261
247
262
248
var c = db.things.find()
263
249
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:
266
252
267
253
.. code-block:: javascript
268
254
@@ -303,16 +289,16 @@ Return More than 20 Documents
303
289
Use Array Operations with the Cursor
304
290
````````````````````````````````````
305
291
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.
307
293
308
- 1. In the MongoDB JavaScript shell, query the collection named ``things``
294
+ 1. In the MongoDB JavaScript shell, query the ``things`` collection
309
295
and assign the resulting cursor object to the ``c`` variable:
310
296
311
297
.. code-block:: javascript
312
298
313
299
var c = db.things.find()
314
300
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``),
316
302
issue the following command:
317
303
318
304
.. code-block:: javascript
@@ -325,46 +311,23 @@ You can treat a cursor like an array. You also can convert a cursor to an array.
325
311
326
312
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
327
313
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.
349
316
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 .
353
320
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`.
358
322
359
323
Query for Specific Documents
360
324
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361
325
362
326
In this procedure, you query for specific documents in the ``things``
363
327
:term:`collection` by passing a "query document" as a parameter to the
364
328
: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`.
368
331
369
332
To query for specific documents, do the following:
370
333
@@ -377,7 +340,7 @@ To query for specific documents, do the following:
377
340
378
341
db.things.find( { name : "mongo" } )
379
342
380
- MongoDB returns the one document that fits this criteria. Your
343
+ MongoDB displays the one document that fits this criteria. Your
381
344
:doc:`ObjectId </object-id>` value will be different:
382
345
383
346
.. code-block:: javascript
@@ -419,17 +382,17 @@ To query for specific documents, do the following:
419
382
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
420
383
421
384
#. 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:
426
390
427
391
.. code-block:: javascript
428
392
429
393
db.things.find( { x : 4 } , { j : true } )
430
394
431
- MongoDB returns the following results, excluding the ``x`` field. The
432
- ``_id`` field is included by default:
395
+ MongoDB returns the following results:
433
396
434
397
.. code-block:: javascript
435
398
@@ -495,6 +458,8 @@ be different:
495
458
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
496
459
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
497
460
461
+ .. todo Add sections on Update and Remove
462
+
498
463
Continuing to Use MongoDB
499
464
-------------------------
500
465
0 commit comments