List Databases in Mongo Shell

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re using MongoDB to store data, it’s important to get acquainted with the Mongo Shell. This command-line interface (CLI) is an interactive tool written in JavaScript that allows you to query or even modify MongoDB collections and their BSON documents. In this article, we’ll demonstrate different ways to list databases in Mongo Shell. With these helpful commands, you’ll be able to see the names of all databases that currently exist on your MongoDB server.

Prerequisites

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:

1
sudo service mongod start

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:

1
sudo service mongod status

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 brew command:

1
brew install mongodb

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:

1
mongo --version

You can also use the mongod --version command to get the MongoDB database version.

Terminal screenshot of mongo Shell version and help information

The --help option can be used with the mongo command to get more information about how to use the interface:

1
mongo --help

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 --verbose option:

1
mongo --verbose

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 -u and -p flags to pass username and password credentials to the command:

1
mongo -u USER_NAME -p sTrOnGpAsSwOrD_1234

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:

1
use testDb

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 db object’s 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:

1
db.testCollection.insert({ "foo" : "bar" })

You should get a nInserted response, indicating the number of documents that were inserted. The response will look like the following:

1
WriteResult({ "nInserted" : 1 })

NOTE: Since the Mongo Shell was built using JavaScript, it adheres to standard JS naming conventions, so it’s advisable to use camelCase when creating databases and collections.

Screenshot of creating a database and collection in mongo Shell

List the databases in Mongo Shell

The basic command to use in Mongo Shell to list the databases on the server is:

1
show dbs

The output of this command will look something like this:

1
2
3
admin   0.000GB
config  0.000GB
local   0.000GB

NOTE: You can also use the show command to list all of the MongoDB server’s collections as well: show collections.

List the MongoDB default databases

If you invoke the database (db) object’s getMongo().getDBNames() method, the Mongo Shell will return all of the default MongoDB databases along with any of your own databases:

1
db.getMongo().getDBNames()

You’ll see results that look like the following:

1
[ "admin", "config", "local" ]

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:

1
db.adminCommand('listDatabases')

The JSON response will look like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
    "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.

Screenshot of list databases mongo shell show databases in terminal

When you’re done, use the exit or quit() command to exit the Mongo Shell and return to your terminal or command prompt window.

Conclusion

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

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.