mongo

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:

  1. Open the Download Center . For the mongo Enterprise Shell, select the MongoDB Enterprise Server tab.

  2. Select your preferred Version and OS from the dropdowns.

  3. 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.

--norc

Prevents the shell from sourcing and evaluating ~/.mongorc.js on start up.

--quiet

Silences output from the shell during the connection process.

--port <port>

Specifies the port where the mongod or mongos instance is listening. If --port is not specified, mongo attempts to connect to port 27017.

--host <hostname>

Specifies the name of the host machine where the mongod or mongos 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 the mongod or mongos to which you are connecting matches the CN or SAN of the mongod or mongos’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. The authSource and replicaSet options, if included in the connection string, will override any corresponding DNS-configured options set in the TXT record. Use of the mongodb+srv: connection string implicitly enables TLS/SSL (normally set with ssl=true) for the client connection. The TLS/SSL option can be turned off by setting ssl=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.

--help , -h

Returns information on the options and use of mongo.

--version

Returns the mongo release number.

--verbose

Increases the verbosity of the output of the shell during the connection process.

--networkMessageCompressors <string>

New in version 3.4.

Enables network compression for communication between this mongo shell and:

You can specify the following compressors:

  • snappy
  • zlib (Available in MongoDB 3.6 or greater)

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 compressors zlib,snappy and the mongod specifies snappy,zlib, messages between mongo shell and mongod uses zlib.

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 compressor zlib and mongod specifies snappy, messages between mongo shell and mongod 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 and mongos disable IPv6 support by default. Specifying --ipv6 when connecting to a mongod/mongos does not enable IPv6 support on the mongod/mongos. For documentation on enabling IPv6 support on the mongod/mongos, see net.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.

--disableJavaScriptJIT

New in version 3.2.

Disables use of the JavaScript engine’s JIT compiler.

--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 type Code. The following example demonstrates the default behavior within the shell: