This version of the documentation is archived and no longer supported.

FAQ: The mongo Shell

How can I enter 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 mongo shell waits for the closing parenthesis, closing brace, or the closing bracket before evaluating the code, as in the following example:

> if ( x > 0 ) {
... count++;
... print (x);
... }

You can exit the line continuation mode if you enter two blank lines, as in the following example:

> if (x > 0

How can I access different databases temporarily?

You can use db.getSiblingDB() method to access another database without switching databases, as in the following example which first switches to the test database and then accesses the sampleDB database from the test database:

use test


Does the mongo shell support tab completion and other keyboard shortcuts?

The mongo shell supports keyboard shortcuts. For example,

  • Use the up/down arrow keys to scroll through command history. See .dbshell 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':


    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 Shell Keyboard Shortcuts

How can I customize the mongo shell prompt?

New in version 1.9.

You can change the mongo shell prompt by setting the prompt variable. This makes it possible to display additional information in the prompt.

Set prompt to any string or arbitrary JavaScript code that returns a string, consider the following examples:

  • Set the shell prompt to display the hostname and the database issued:

    var host = db.serverStatus().host;
    var prompt = function() { return db+"@"+host+"> "; }

    The mongo shell prompt should now reflect the new prompt:

  • Set the shell prompt to display the database statistics:

    var prompt = function() {
                    return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";

    The mongo shell prompt should now reflect the new prompt:

    Uptime:1052 Documents:25024787 >

You can add the logic for the prompt in the .mongorc.js file to set the prompt each time you start up the mongo shell.

Can I edit long shell operations with an external text editor?

You can use your own editor in the mongo shell by setting the EDITOR environment variable before starting the mongo shell. Once in the mongo shell, you can edit with the specified editor by typing edit <variable> or edit <function>, as in the following example:

  1. Set the EDITOR variable from the command line prompt:

  2. Start the mongo shell:

  3. Define a function myFunction:

    function myFunction () { }
  4. Edit the function using your editor:

    edit myFunction

    The command should open the vim edit session. Remember to save your changes.

  5. Type myFunction to see the function definition:


    The result should be the changes from your saved edit:

    function myFunction() {
        print("This was edited");