List Databases in Mongo Shell
Before we proceed with the examples in this tutorial, there are a few important prerequisites that need to be in place. First, make sure that a MongoDB server is installed and running on your system. If you’re running Linux, you can use the following command to start the MongoDB server:
You’ll be prompted for a root privilege password; after you input the password, just hit the ENTER button.
You can use the following to check if the MongoDB server is running:
NOTE: You can also use
/etc/init.d/mongod status to check the status of MongoDB.
On macOS X, you can install MongoDB using Homebrew’s
Using the Mongo Shell CLI
Once you’ve confirmed that MongoDB is installed and running, you can initialize the MongoDB status by starting a Mongo Shell instance in the terminal. Use the following command to have the Mongo Shell CLI return its version number:
You can also use the
mongod --version command to get the MongoDB database version.
--help option can be used with the
mongo command to get more information about how to use the interface:
In the next section, we’ll start using the Mongo Shell to query and perform database operations in MongoDB. This interactive command-line interface allows us to perform different tasks, including listing all of our databases.
Access Mongo Shell
To enter the client interface, just type
mongo on the command line. If you’d like more verbose feedback on the commands you execute within the shell, use the
You can also connect to the Mongo Shell by connecting to the port process directly, as seen in the following example:
mongo 27017. Use the
-p flags to pass username and password credentials to the command:
This command will create an instance that it will use to start the Mongo Shell. You may notice that it will give you the version of the Mongo Shell you are currently using. Once you’re successfully in the shell interface, you can connect and start working with MongoDB.
Create a MongoDB database and collection
Now that you’ve entered the Mongo Shell, you can type
use to access a database namespace:
The command shown above should return
switched to db testDb. You can also use the
db command to see your current database. When you’re done with a database, use the
logout() method to disconnect from it.
NOTE: You can use the database’s optional
auth() method to pass credentials and obtain authorization to modify a database and its collections. The following is an example:
db.auth( "root", "1234" ).
Insert a document into a MongoDB collection
MongoDB will create a collection on the fly as you insert documents into it. We can see how this works in the following example:
You should get a
nInserted response, indicating the number of documents that were inserted. The response will look like the following:
camelCase when creating databases and collections.
List the databases in Mongo Shell
The basic command to use in Mongo Shell to list the databases on the server is:
The output of this command will look something like this:
NOTE: You can also use the
show command to list all of the MongoDB server’s collections as well:
List the MongoDB default databases
If you invoke the database (
getMongo().getDBNames() method, the Mongo Shell will return all of the default MongoDB databases along with any of your own databases:
You’ll see results that look like the following:
Use Mongo CLI to list databases as a JSON response
Another way to list databases in the Mongo Shell is to run a specific command that executes an admin command. This will show more information about the database in a human-readable JSON format:
The JSON response will look like this :
"databases" : [
"name" : "admin",
"sizeOnDisk" : 40960,
"empty" : false
"name" : "config",
"sizeOnDisk" : 98304,
"empty" : false
"name" : "local",
"sizeOnDisk" : 73728,
"empty" : false
"totalSize" : 212992,
"ok" : 1
Returning the result in JSON format allows you to see more detailed information about the databases when using the Mongo Shell.
When you’re done, use the
quit() command to exit the Mongo Shell and return to your terminal or command prompt window.
When you’re working with MongoDB, it’s important to know which databases reside on your server. Fortunately, there are multiple ways to obtain this information when using the interactive Mongo Shell. In this article, we showed a few different ways to list databases in the Mongo Shell. With our instructions and examples, you’ll be able to obtain the information you need about your own MongoDB instance.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started