On this page
mongo
On this page
Description
mongo
is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for systems administrators as well as a way for developers to test queries and operations directly with the database. mongo
also provides a fully functional JavaScript environment for use with a MongoDB.
The mongo
shell is included as part of the MongoDB Server installation. MongoDB also provides the mongo
shell as a standalone package. To download the standalone mongo
shell package:
Open the Download Center . For the
mongo
Enterprise Shell, select the MongoDB Enterprise Server tab.Select your preferred Version and OS from the dropdowns.
Select
Shell
from the Package dropdown and click Download to start downloading the package.If the
Shell
option is unavailable for the selected OS and Version, contact MongoDB Technical Support for assistance.
Note
Starting in MongoDB 3.6.15, the mongo
shell displays a warning message when connected to non-genuine MongoDB instances as these instances may behave differently from the official MongoDB instances; e.g. missing or incomplete features, different feature behaviors, etc.
Options
Core Options
--shell
-
Enables the shell interface. If you invoke the mongo command and specify a JavaScript file as an argument, or use
--eval
to specify JavaScript on the command line, the--shell
option provides the user with a shell prompt after the file finishes executing.
--nodb
-
Prevents the shell from connecting to any database instances. Later, to connect to a database within the shell, see Opening New Connections.
--port
<port>
-
Specifies the port where the
mongod
ormongos
instance is listening. If--port
is not specified, mongo attempts to connect to port27017
.
--host
<hostname>
-
Specifies the name of the host machine where the
mongod
ormongos
is running. If this is not specified, mongo attempts to connect to a MongoDB process running on the localhost.To connect to a replica set, specify the
replica set name
and a seed list of set members. Use the following form:<replSetName>/<hostname1><:port>,<hostname2><:port>,<...>
For TLS/SSL connections (
--ssl
), mongo verifies that the hostname of themongod
ormongos
to which you are connecting matches the CN or SAN of themongod
ormongos
’s--sslPEMKeyFile
certificate. If the hostname does not match the CN/SAN, mongo will fail to connect.For DNS seedlist connections , specify the connection protocol as
mongodb+srv
, followed by the DNS SRV hostname record and any options. TheauthSource
andreplicaSet
options, if included in the connection string, will override any corresponding DNS-configured options set in the TXT record. Use of themongodb+srv:
connection string implicitly enables TLS/SSL (normally set withssl=true
) for the client connection. The TLS/SSL option can be turned off by settingssl=false
in the query string.Example:
mongodb+srv://server.example.com/?connectionTimeout=3000ms
New in version 3.6.
--eval
<javascript>
-
Evaluates a JavaScript expression that is specified as an argument. mongo does not load its own environment when evaluating code. As a result many options of the shell environment are not available.
--username
<username>
,
-u
<username>
-
Specifies a username with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--password
and--authenticationDatabase
options.
--password
<password>
,
-p
<password>
-
Specifies a password with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--username
and--authenticationDatabase
options. To force mongo to prompt for a password, enter the--password
option as the last option and leave out the argument.
--networkMessageCompressors
<string>
-
New in version 3.4.
Enables network compression for communication between this mongo shell and:
You can specify the following compressors:
Important
Messages are compressed when both parties enable network compression. Otherwise, messages between the parties are uncompressed.
If you specify multiple compressors, then the order in which you list the compressors matter as well as the communication initiator. For example, if a
mongo
shell specifies the following network compressorszlib,snappy
and themongod
specifiessnappy,zlib
, messages betweenmongo
shell andmongod
useszlib
.If the parties do not share at least one common compressor, messages between the parties are uncompressed. For example, if a
mongo
shell specifies the network compressorzlib
andmongod
specifiessnappy
, messages betweenmongo
shell andmongod
are not compressed.
--ipv6
-
Enables IPv6 support. mongo disables IPv6 by default.
To connect to a MongoDB cluster via IPv6, you must specify both
--ipv6
and--host <mongod/mongos IPv6 address>
when starting the mongo shell.mongod
andmongos
disable IPv6 support by default. Specifying--ipv6
when connecting to amongod/mongos
does not enable IPv6 support on themongod/mongos
. For documentation on enabling IPv6 support on themongod/mongos
, seenet.ipv6
.
<db
name>
-
Specifies the name of the database to connect to. For example:
mongo admin
The above command will connect the mongo shell to the admin database of the MongoDB deployment running on the local machine. You may specify a remote database instance, with the resolvable hostname or IP address. Separate the database name from the hostname using a
/
character. See the following examples:mongo mongodb1.example.net/test mongo mongodb1/admin mongo 10.8.8.10/test
This syntax is the only way to connect to a specific database.
To specify alternate hosts and a database, you must use this syntax and cannot use
--host
or--port
.
--disableJavaScriptProtection
-
New in version 3.4.
Allows fields of type javascript and javascriptWithScope to be automatically marshalled to JavaScript functions in the
mongo
shell.With the
--disableJavaScriptProtection
flag set, it is possible to immediately execute JavaScript functions contained in documents. The following example demonstrates this behavior within the shell:> db.test.insert({ _id: 1, jsFunc: function(){ print("hello") } } ) WriteResult({ "nInserted" : 1 }) > var doc = db.test.findOne({ _id: 1 }) > doc { "_id" : 1, "jsFunc" : function (){ print ("hello") } } > typeof doc.jsFunc function > doc.jsFunc() hello
The default behavior (when
mongo
starts without the--disableJavaScriptProtection
flag) is to convert embedded JavaScript functions to the non-executable MongoDB shell typeCode
. The following example demonstrates the default behavior within the shell: