- Reference >
- MongoDB Package Components >
mongo
mongo¶
On this page
Description¶
-
mongo¶
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. This
document addresses the basic invocation of the mongo shell
and an overview of its usage.
Interface¶
Options¶
-
--shell¶ Enables the shell interface after evaluating a JavaScript file. If you invoke the
mongocommand 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.
-
--norc¶ Prevents the shell from sourcing and evaluating
~/.mongorc.json start up.
-
--quiet¶ Silences output from the shell during the connection process.
-
--port<port>¶ Specifies the port where the
mongodormongosinstance is listening. Unless specifiedmongoconnects tomongodinstances on port27017, which is the defaultmongodport.
-
--host<hostname>¶ specifies the host where the
mongodormongosis running to connect to as<hostname>. By defaultmongowill attempt 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:
-
--eval<javascript>¶ Evaluates a JavaScript expression specified as an argument to this option.
mongodoes 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 to authenticate to the MongoDB instance. Use in conjunction with the
--passwordoption to supply a password. If you specify a username and password but the default database or the specified database do not require authentication,mongowill exit with an exception.
-
--password<password>,-p<password>¶ Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the
--usernameoption to supply a username. If you specify a--usernameand do not pass an argument to the--passwordoption,mongowill prompt for a password interactively, if themongodormongosrequires authentication.If you chose not to provide an argument so that
mongowill prompt for a password,--passwordmust be the last option.
-
--authenticationDatabase<dbname>¶ New in version 2.4.
Specifies the database that holds the user’s (e.g
--username) credentials.By default,
mongoassumes that the database specified to the--dbargument holds the user’s credentials, unless you specify--authenticationDatabase.See
userSource, system.users Privilege Documents and User Privilege Roles in MongoDB for more information about delegated authentication in MongoDB.
-
--authenticationMechanism<name>¶ New in version 2.4.
Specifies the authentication mechanism. By default, the authentication mechanism is
MONGODB-CR, which is the MongoDB challenge/response authentication mechanism. In MongoDB Enterprise,mongoalso includes support forGSSAPIto handle Kerberos authentication.See Deploy MongoDB with Kerberos Authentication for more information about Kerberos authentication.
-
--sslPEMKeyFile<filename>¶ New in version 2.4.
Note
The default distribution of MongoDB does not contain support for SSL. To use SSL you can either compile MongoDB with SSL support or use MongoDB Enterprise. See Connect to MongoDB with SSL for more information about SSL and MongoDB.
Specifies the
.pemfile that contains both the SSL certificate and key. Specify the file name of the.pemfile using relative or absolute pathsRequired when using the
--ssloption if themongodormongoshassslCAFileenabled withoutsslWeakCertificateValidation.
-
--sslPEMKeyPassword<value>¶ New in version 2.4.
Note
The default distribution of MongoDB does not contain support for SSL. To use SSL you can either compile MongoDB with SSL support or use MongoDB Enterprise. See Connect to MongoDB with SSL for more information about SSL and MongoDB.
Specifies the password to decrypt the root certificate chain specified by
--sslPEMKeyFile.Only required if the certificate-key file is encrypted.
-
--sslCAFile<filename>¶ New in version 2.4.
Note
The default distribution of MongoDB does not contain support for SSL. To use SSL you can either compile MongoDB with SSL support or use MongoDB Enterprise. See Connect to MongoDB with SSL for more information about SSL and MongoDB.
Specifies the
.pemfile that contains the certificate from the Certificate Authority. Specify the file name of the.pemfile using relative or absolute paths
-
--help,-h¶ Returns a basic help and usage text.
-
--version¶ Returns the version of the shell.
-
--verbose¶ Increases the verbosity of the output of the shell during the connection process.
-
--ipv6¶ Enables IPv6 support that allows
mongoto connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, includingmongo, disable IPv6 support by default.
-
<dbaddress>¶ Specifies the “database address” of the database to connect to. For example:
The above command will connect the
mongoshell to the admin database 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:
-
<file.js>¶ Specifies a JavaScript file to run and then exit. Generally this should be the last option specified.
Optional
To specify a JavaScript file to execute and allow
mongoto prompt you for a password using--password, pass the filename as the first parameter with--usernameand--passwords the last options as in the following:Use the
--shelloption to return to a shell after the file finishes running.
Files¶
~/.dbshellmongomaintains a history of commands in the.dbshellfile.Note
mongodoes not recorded interaction related to authentication in the history file, includingauthenticateanddb.addUser().Warning
Versions of Windows
mongo.exeearlier than 2.2.0 will save the .dbshell file in themongo.exeworking directory.
~/.mongorc.jsmongowill read the.mongorc.jsfile from the home directory of the user invokingmongo. In the file, users can define variables, customize themongoshell prompt, or update information that they would like updated every time they launch a shell. If you use the shell to evaluate a JavaScript file or expression either on the command line with--evalor by specifying a .js file to mongo,mongowill read the.mongorc.jsfile after the JavaScript has finished processing.Specify the
--norcoption to disable reading.mongorc.js./tmp/mongo_edit<time_t>.js- Created by
mongowhen editing a file. If the file exists,mongowill append an integer from1to10to the time value to attempt to create a unique file. %TEMP%mongo_edit<time_t>.js- Created by
mongo.exeon Windows when editing a file. If the file exists,mongowill append an integer from1to10to the time value to attempt to create a unique file.
Environment¶
-
EDITOR¶ Specifies the path to an editor to use with the
editshell command. A JavaScript variableEDITORwill override the value ofEDITOR.
-
HOME¶ Specifies the path to the home directory where
mongowill read the.mongorc.jsfile and write the.dbshellfile.
Keyboard Shortcuts¶
The mongo shell supports the following keyboard shortcuts:
[1]
| Keybinding | Function |
|---|---|
| Up arrow | Retrieve previous command from history |
| Down-arrow | Retrieve next command from history |
| Home | Go to beginning of the line |
| End | Go to end of the line |
| Tab | Autocomplete method/command |
| Left-arrow | Go backward one character |
| Right-arrow | Go forward one character |
| Ctrl-left-arrow | Go backward one word |
| Ctrl-right-arrow | Go forward one word |
| Meta-left-arrow | Go backward one word |
| Meta-right-arrow | Go forward one word |
| Ctrl-A | Go to the beginning of the line |
| Ctrl-B | Go backward one character |
| Ctrl-C | Exit the mongo shell |
| Ctrl-D | Delete a char (or exit the mongo shell) |
| Ctrl-E | Go to the end of the line |
| Ctrl-F | Go forward one character |
| Ctrl-G | Abort |
| Ctrl-J | Accept/evaluate the line |
| Ctrl-K | Kill/erase the line |
Ctrl-L or type cls |
Clear the screen |
| Ctrl-M | Accept/evaluate the line |
| Ctrl-N | Retrieve next command from history |
| Ctrl-P | Retrieve previous command from history |
| Ctrl-R | Reverse-search command history |
| Ctrl-S | Forward-search command history |
| Ctrl-T | Transpose characters |
| Ctrl-U | Perform Unix line-discard |
| Ctrl-W | Perform Unix word-rubout |
| Ctrl-Y | Yank |
| Ctrl-Z | Suspend (job control works in linux) |
| Ctrl-H | Backward-delete a character |
| Ctrl-I | Complete, same as Tab |
| Meta-B | Go backward one word |
| Meta-C | Capitalize word |
| Meta-D | Kill word |
| Meta-F | Go forward one word |
| Meta-L | Change word to lowercase |
| Meta-U | Change word to uppercase |
| Meta-Y | Yank-pop |
| Meta-Backspace | Backward-kill word |
| Meta-< | Retrieve the first command in command history |
| Meta-> | Retrieve the last command in command history |
| [1] | MongoDB accommodates multiple keybinding.
Since 2.0, mongo includes support for basic emacs
keybindings. |
Use¶
Typically users invoke the shell with the mongo command at
the system prompt. Consider the following examples for other
scenarios.
To connect to a database on a remote host using authentication and a non-standard port, use the following form:
Alternatively, consider the following short form:
Replace <user>, <pass>, and <host> with the appropriate
values for your situation and substitute or omit the --port
as needed.
To execute a JavaScript file without evaluating the ~/.mongorc.js
file before starting a shell session, use the following form:
To execute a JavaScript file with authentication, with password prompted rather than provided on the command-line, use the following form:
To print return a query as JSON, from the system prompt using
the --eval option, use the following form:
Use single quotes (e.g. ') to enclose the JavaScript, as well as
the additional JavaScript required to generate this output.