- Frequently Asked Questions >
- FAQ: The
mongoShell
FAQ: The mongo Shell¶
On this page
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:
You can exit the line continuation mode if you enter two blank lines, as in the following example:
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:
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
.dbshellfile.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:
The
mongoshell prompt should now reflect the new prompt:Set the shell prompt to display the database statistics:
The
mongoshell prompt should now reflect the new prompt:
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:
Set the
EDITORvariable from the command line prompt:Start the
mongoshell:Define a function
myFunction:Edit the function using your editor:
The command should open the
vimedit session. Remember to save your changes.Type
myFunctionto see the function definition:The result should be the changes from your saved edit: