-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmongo.txt
More file actions
421 lines (282 loc) · 12.9 KB
/
Copy pathmongo.txt
File metadata and controls
421 lines (282 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
===================
The ``mongo`` Shell
===================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
The :binary:`~bin.mongo` shell is an interactive JavaScript interface to
MongoDB. You can use the :binary:`~bin.mongo` shell to query and update
data as well as perform administrative operations.
Download the ``mongo`` Shell
----------------------------
.. include:: /includes/fact-download-mongo-shell.rst
Start the ``mongo`` Shell and Connect to MongoDB
------------------------------------------------
Once you have downloaded the :binary:`~bin.mongo` shell, you can use it
to connect to your running MongoDB server.
.. note::
.. include:: /includes/extracts/fact-mongoshell-emulation.rst
Prerequisites
~~~~~~~~~~~~~
- The MongoDB server must be installed and running before you can
connect to it from the :binary:`~bin.mongo` shell. Follow the steps in
the :doc:`installation tutorial for your platform </installation>`
to install and start the MongoDB server if required.
- Once you have verified that the :binary:`~bin.mongod` server is
running, open a terminal window (or a command prompt for Windows) and
go to your ``<mongo shell installation dir>`` directory:
.. code-block:: sh
cd <mongo shell installation dir>
.. tip::
Adding your ``<mongo shell installation dir>`` to the ``PATH``
environment variable allows you to type ``mongo`` directly instead
of having to first go to the ``<mongo shell installation dir>``
directory or specify the full path to the binary. Alternatively, you
can copy the ``mongo`` shell to a location on your filesystem that
is already present in your ``PATH``, such as ``/usr/bin`` on Linux.
macOS Security Verification
~~~~~~~~~~~~~~~~~~~~~~~~~~~
*For macOS users:*
.. include:: /includes/extracts/macos-prevent-launch-mongo.rst
Local MongoDB Instance on Default Port
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can run :binary:`~bin.mongo` shell without any command-line options
to connect to a :doc:`MongoDB </reference/program/mongod>` instance
running on your **localhost** with **default port** 27017:
.. code-block:: sh
mongo
Local MongoDB Instance on a Non-default Port
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To explicitly specify the port, include the :option:`--port <mongo
--port>` command-line option. For example, to connect to a MongoDB
instance running on localhost with a non-default port 28015:
.. code-block:: sh
mongo --port 28015
MongoDB Instance on a Remote Host
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To explicitly specify the hostname and/or port,
- You can specify a :doc:`connection string
</reference/connection-string>`. For example, to connect to a MongoDB
instance running on a remote host machine with default port 27017:
.. code-block:: sh
mongo "mongodb://mongodb0.example.com:27017"
- You can use the command-line option :option:`--host \<host\>:\<port\>
<mongo --host>`. For example, to connect to a MongoDB instance
running on a remote host machine:
.. code-block:: sh
mongo --host mongodb0.example.com:27017
- You can use the :option:`--host \<host\> <mongo --host>` and
:option:`--port \<port\> <mongo --port>` command-line options. For
example, to connect to a MongoDB instance running on a remote host
machine:
.. code-block:: sh
mongo --host mongodb0.example.com --port 27017
MongoDB Instance with Authentication
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To connect to a MongoDB instance requires authentication:
- You can specify the username, authentication database, and optionally
the password in the :doc:`connection string
</reference/connection-string>`. For example, to connect and
authenticate to a remote MongoDB instance as user ``alice``:
.. note::
If you do not specify the password in the connection string, the
shell will prompt for the password.
.. code-block:: sh
mongo "mongodb://alice@mongodb0.examples.com:27017/?authSource=admin"
- You can use the :option:`--username \<user\> <mongo --username>` and
:option:`--password <mongo --password>`,
:option:`--authenticationDatabase \<db\> <mongo
--authenticationDatabase>` command-line options. For example, to
connect and authenticate to a remote MongoDB instance as user
``alice``:
.. note::
If you specify ``--password`` without the user's password, the
shell will prompt for the password.
.. code-block:: sh
mongo --username alice --password --authenticationDatabase admin --host mongodb0.examples.com --port 28015
Connect to a MongoDB Replica Set
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To connect to a replica set:
- You can specify the replica set name and members in the
:doc:`connection string </reference/connection-string>`.
.. code-block:: sh
mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
- If using the :ref:`connections-dns-seedlist`, you can specify the
connection string:
.. code-block:: sh
mongo "mongodb+srv://server.example.com/"
.. note::
Use of the ``+srv`` connection string modifier automatically sets
the ssl option to true for the connection.
- You can specify the replica set name and members from the
:option:`--host \<replica set
name\>/\<host1\>:\<port1\>,\<host2\>:\<port2\>,... <mongo --host>`
command-line option. For example, to connect to replica set named
``replA``:
.. code-block:: sh
mongo --host replA/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
TLS/SSL Connection
~~~~~~~~~~~~~~~~~~
For TLS/SSL connections,
- You can specify the ``ssl=true`` option in the
:doc:`connection string </reference/connection-string>`.
.. code-block:: sh
mongo "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA&ssl=true"
- If using the :ref:`connections-dns-seedlist`, you can include the
``+srv`` connection string modifier:
.. code-block:: sh
mongo "mongodb+srv://server.example.com/"
.. note::
Use of the ``+srv`` connection string modifier automatically sets
the ssl option to true for the connection.
- You can specify :option:`--ssl <mongo --ssl>` command-line option.
For example, to connect to replica set named ``replA``:
.. code-block:: sh
mongo --ssl --host replA/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
.. seealso::
For more information on the options used in the connection examples
as well as other options, see :doc:`mongo reference
</reference/program/mongo>` and :ref:`examples of starting up mongo
<mongo-usage-examples>`.
.. _mongo-shell-executing-queries:
Working with the ``mongo`` Shell
--------------------------------
To display the database you are using, type ``db``:
.. code-block:: sh
db
The operation should return ``test``, which is the default database.
To switch databases, issue the ``use <db>`` helper, as in the
following example:
.. code-block:: javascript
use <database>
See also :method:`db.getSiblingDB()` method to access a
different database from the current database without switching your
current database context (i.e. ``db``).
To list the databases available to the user, use the helper ``show
dbs``. [#access]_
You can switch to non-existing databases. When you first store data in
the database, such as by creating a collection, MongoDB creates the
database. For example, the following creates both the database
``myNewDatabase`` and the :term:`collection` ``myCollection`` during
the :method:`~db.collection.insertOne()` operation:
.. code-block:: javascript
use myNewDatabase
db.myCollection.insertOne( { x: 1 } );
The :method:`db.myCollection.insertOne() <db.collection.insertOne()>` is one
of the :doc:`methods available in the mongo shell </reference/method>`.
- ``db`` refers to the current database.
- ``myCollection`` is the name of the collection.
If the :binary:`~bin.mongo` shell does not accept the name of a collection,
you can use the alternative :method:`db.getCollection()` syntax.
For instance, if a collection name contains a space or hyphen, starts
with a number, or conflicts with a built-in function:
.. code-block:: javascript
db.getCollection("3 test").find()
db.getCollection("3-test").find()
db.getCollection("stats").find()
.. include:: /includes/fact-mongo-prompt-size.rst
For more documentation of basic MongoDB operations in the
:binary:`~bin.mongo` shell, see:
- :doc:`Getting Started Guide for the mongo Shell
</tutorial/getting-started>`
- :doc:`/tutorial/insert-documents`
- :doc:`/tutorial/query-documents`
- :doc:`/tutorial/update-documents`
- :doc:`/tutorial/remove-documents`
- :doc:`/reference/method`
.. [#access]
If the deployment runs with access control, the operation
returns different values based on user privileges. See
:ref:`listDatabases Behavior <listDatabases-behavior>` for details.
Format Printed Results
~~~~~~~~~~~~~~~~~~~~~~
The :method:`db.collection.find()` method returns a :term:`cursor` to
the results; however, in the :binary:`~bin.mongo` shell, if the returned
cursor is not assigned to a variable using the ``var`` keyword, then
the cursor is automatically iterated up to 20 times to print up to the
first 20 documents that match the query. The :binary:`~bin.mongo` shell
will prompt ``Type it`` to iterate another 20 times.
To format the printed result, you can add the ``.pretty()`` to the
operation, as in the following:
.. code-block:: javascript
db.myCollection.find().pretty()
In addition, you can use the following explicit print methods in the
:binary:`~bin.mongo` shell:
- ``print()`` to print without formatting
- ``print(tojson(<obj>))`` to print with :term:`JSON` formatting and
equivalent to ``printjson()``
- ``printjson()`` to print with :term:`JSON` formatting and equivalent
to ``print(tojson(<obj>))``
For more information and examples on cursor handling in the
:binary:`~bin.mongo` shell, see :doc:`/tutorial/iterate-a-cursor`. See also
:ref:`mongo-shell-help-cursor` for list of cursor help in the
:binary:`~bin.mongo` shell.
Multi-line Operations in the ``mongo`` Shell
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you end a line with an open parenthesis (``'('``), an open brace
(``'{'``), or an open bracket (``'['``), then the subsequent lines start
with ellipsis (``"..."``) until you enter the corresponding closing
parenthesis (``')'``), the closing brace (``'}'``) or the closing
bracket (``']'``). The :binary:`~bin.mongo` shell waits for the closing
parenthesis, closing brace, or the closing bracket before evaluating
the code, as in the following example:
.. code-block:: javascript
> if ( x > 0 ) {
... count++;
... print (x);
... }
You can exit the line continuation mode if you enter two blank
lines, as in the following example:
.. code-block:: javascript
> if (x > 0
...
...
>
Tab Completion and Other Keyboard Shortcuts
-------------------------------------------
The :binary:`~bin.mongo` shell supports keyboard shortcuts. For example,
- Use the up/down arrow keys to scroll through command history. See
:ref:`.dbshell <mongo-dbshell-file>` documentation for more
information on the ``.dbshell`` file.
- Use ``<Tab>`` to autocomplete or to list the completion
possibilities, as in the following example which uses ``<Tab>`` to
complete the method name starting with the letter ``'c'``:
.. code-block:: javascript
db.myCollection.c<Tab>
Because there are many collection methods starting with the letter
``'c'``, the ``<Tab>`` will list the various methods that start with
``'c'``.
For a full list of the shortcuts, see :ref:`Shell Keyboard Shortcuts
<mongo-keyboard-shortcuts>`
``.mongorc.js`` File
--------------------
When starting, :binary:`~bin.mongo` checks the user's :envvar:`HOME`
directory for a JavaScript file named :ref:`.mongorc.js
<mongo-mongorc-file>`. If found, :binary:`~bin.mongo` interprets the
content of :file:`.mongorc.js` before displaying the prompt for the
first time. If you use the shell to evaluate a JavaScript file or
expression, either by using the :option:`--eval <mongo --eval>`
option on the command line or by specifying :ref:`a .js file to
mongo <mongo-shell-file>`, :binary:`~bin.mongo` will read the
``.mongorc.js`` file *after* the JavaScript has finished processing.
You can prevent ``.mongorc.js`` from being loaded by using the
:option:`--norc <mongo --norc>` option.
.. _mongo-shell-exit:
Exit the Shell
--------------
To exit the shell, type ``quit()`` or use the ``<Ctrl-C>`` shortcut.
.. seealso::
- :doc:`Getting Started Guide for the mongo Shell
</tutorial/getting-started>`
- :binary:`~bin.mongo` Reference Page
.. class:: hidden
.. toctree::
:titlesonly:
/tutorial/configure-mongo-shell
/tutorial/access-mongo-shell-help
/tutorial/write-scripts-for-the-mongo-shell
/core/shell-types
/reference/mongo-shell