- Using the
mongo
Shell > - Getting Started with the
mongo
Shell
Getting Started with the mongo
Shell¶
On this page
This document provides a basic introduction to using the
mongo
shell. See Installing MongoDB for instructions on
installing MongoDB for your system.
Start the mongo
Shell¶
To start the mongo
shell and connect to your MongoDB instance running on localhost with default port:
Go to your
<mongodb installation dir>
:Type
./bin/mongo
to startmongo
:If you have added the
<mongodb installation dir>/bin
to thePATH
environment variable, you can just typemongo
instead of./bin/mongo
.To display the database you are using, type
db
:The command should return
test
, which is the default database. To switch databases, issue theuse <db>
command, as in the following example:To list the available databases, use the command
show dbs
. See also How can I access different databases temporarily? to access a different database from the current database without switching your current database context (i.e.db.
.)
To start the mongo
shell with other options, see
examples of starting up mongo and
mongo reference which provides details on the
available options.
Note
When starting, mongo
checks the user’s HOME
directory for a JavaScript file named .mongorc.js. If found, mongo
interprets the
content of .mongorc.js
before displaying the prompt for the
first time. If you use the shell to evaluate a JavaScript file or
expression, either by using the --eval
option on the
command line or by specifying a .js file to mongo, mongo
will read the .mongorc.js
file after the JavaScript has finished processing.
Executing Queries¶
From the mongo
shell, you can use the shell methods to run queries, as in the following example:
The
db
refers to the current database.The
<collection>
is the name of the collection to query. See Collection Help to list the available collections.If the
mongo
shell does not accept the name of the collection, for instance if the name contains a space, hyphen, or starts with a number, you can use an alternate syntax to refer to the collection, as in the following:The
find()
method is the JavaScript method to retrieve documents from<collection>
. Thefind()
method returns a cursor to the results; however, in themongo
shell, if the returned cursor is not assigned to a variable, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. Themongo
shell will promptType it
to iterate another 20 times.You can set the
DBQuery.shellBatchSize
attribute to change the number of iteration from the default value20
, as in the following example which sets it to10
:For more information and examples on cursor handling in the
mongo
shell, see Cursors.See also Cursor Help for list of cursor help in the
mongo
shell.
For more documentation of basic MongoDB operations in the
mongo
shell, see:
Print¶
The mongo
shell automatically prints the results of the
find()
method if the returned cursor
is not assigned to a variable. To format the result, you can add the
.pretty()
to the operation, as in the following:
In addition, you can use the following explicit print methods in the
mongo
shell:
Use a Custom Prompt¶
You may modify the content of the prompt by creating the variable
prompt
in the shell. The prompt variable can hold strings as well
as any arbitrary JavaScript. If prompt
holds a function that returns a
string, mongo
can display dynamic information in each
prompt. Consider the following examples:
Example
Create a prompt with the number of commands issued in the current session, define the following variables:
The prompt would then resemble the following:
Example
To create a mongo
shell prompt in the form of
<database>@<hostnane>$
define the following variables:
The prompt would then resemble the following:
Example
To create a mongo
shell prompt that contains the system
up time and the number of documents in the current database,
define the following prompt variable:
The prompt would then resemble the following:
Use an External Editor in the mongo
Shell¶
New in version 2.2.
In the mongo
shell you can use the edit
operation to
edit a function or variable in an external editor. The edit
operation uses the value of your environments EDITOR
variable.
At your system prompt you can define the EDITOR
variable and start
mongo
with the following two operations:
Then, consider the following example shell session:
Note
As mongo
shell interprets code edited in an external
editor, it may modify code in functions, depending on the
JavaScript compiler. For mongo
may convert 1+1
to
2
or remove comments. The actual changes affect only the
appearance of the code and will vary based on the version of
JavaScript used but will not affect the semantics of the code.
Exit the Shell¶
To exit the shell, type quit()
or use the <Ctrl-c>
shortcut.