The mongo
Shell
On this page
- Start the
mongo
Shell and Connect to MongoDB - Working with the
mongo
Shell - Tab Completion and Other Keyboard Shortcuts
.mongorc.js
File- Exit the Shell
The mongo
shell is an interactive JavaScript interface to MongoDB. You can use the mongo
shell to query and update data as well as perform administrative operations.
The mongo
shell is a component of the MongoDB distributions. Once you have installed and have started MongoDB, connect the mongo
shell to your running MongoDB instance.
Start the mongo
Shell and Connect to MongoDB
Prerequisites
Ensure that MongoDB is running before attempting to start the mongo
shell.
Open a terminal window (or a command prompt for Windows) and go to your <mongodb installationdir>/bin
directory:
copy
copied
cd <mongodb installation dir>/bin
TIP
Adding your <mongodb installation dir>/bin
to the PATH
environment variable allows you to type mongo
instead of having to go to the <mongodb installation dir>/bin
directory or specify the full path to the binary.
Local MongoDB Instance on Default Port
You can run mongo
shell without any command-line options to connect to a MongoDB instance running on your localhost with default port 27017:
copy
copied
mongo
Local MongoDB Instance on a Non-default Port
To explicitly specify the port, include the --port
command-line option. For example, to connect to a MongoDB instance running on localhost with a non-default port 28015:
copy
copied
mongo --port 28015
MongoDB Instance on a Remote Host
To explicitly specify the hostname and/or port,
-
You can specify a connection string. For example, to connect to a MongoDB instance running on a remote host machine with default port 27017:
copycopied
mongo "mongodb://mongodb0.example.com:27017"
-
You can use the command-line option <