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
mongoEnterprise Shell, select the MongoDB Enterprise Server tab.Select your preferred Version and OS from the dropdowns.
Select
Shellfrom the Package dropdown and click Download to start downloading the package.If the
Shelloption 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
--evalto specify JavaScript on the command line, the--shelloption 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
mongodormongosinstance is listening. If--portis not specified, mongo attempts to connect to port27017.
--host<hostname>-
Specifies the name of the host machine where the
mongodormongosis 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 nameand 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 themongodormongosto which you are connecting matches the CN or SAN of themongodormongos’s--sslPEMKeyFilecertificate. 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. TheauthSourceandreplicaSetoptions, 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=falsein the query string.Example:
mongodb+srv://server.example.com/?connectionTimeout=3000msNew 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
--passwordand--authenticationDatabaseoptions.
--password<password>,-p<password>-
Specifies a password with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--usernameand--authenticationDatabaseoptions. To force mongo to prompt for a password, enter the--passwordoption 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
mongoshell specifies the following network compressorszlib,snappyand themongodspecifiessnappy,zlib, messages betweenmongoshell andmongoduseszlib.If the parties do not share at least one common compressor, messages between the parties are uncompressed. For example, if a
mongoshell specifies the network compressorzlibandmongodspecifiessnappy, messages betweenmongoshell andmongodare not compressed.
--ipv6-
Enables IPv6 support. mongo disables IPv6 by default.
To connect to a MongoDB cluster via IPv6, you must specify both
--ipv6and--host <mongod/mongos IPv6 address>when starting the mongo shell.mongodandmongosdisable IPv6 support by default. Specifying--ipv6when connecting to amongod/mongosdoes not enable IPv6 support on themongod/mongos. For documentation on enabling IPv6 support on themongod/mongos, seenet.ipv6.
<dbname>-
Specifies the name of the database to connect to. For example:
mongo adminThe 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/testThis 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
--hostor--port.
--disableJavaScriptProtection-
New in version 3.4.
Allows fields of type javascript and javascriptWithScope to be automatically marshalled to JavaScript functions in the
mongoshell.With the
--disableJavaScriptProtectionflag 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() helloThe default behavior (when
mongostarts without the--disableJavaScriptProtectionflag) is to convert embedded JavaScript functions to the non-executable MongoDB shell typeCode. The following example demonstrates the default behavior within the shell: