Mongo Shell is a powerful command-line interface for managing and administering your MongoDB databases. It provides an intuitive and interactive command line utility that allows you to manage the databases, collections, and individual documents.
Therefore, learning how to use the Mongo Shell is critical for any MongoDB user. In this tutorial, we will help you master the Mongo Shell commands by discussing how to view all the databases in your server.
Let’s jump in.
Requirements
Before proceeding, it is good to ensure that you met the following requirements:
- MongoDB Server
- Mongo Shell tools
With the given requirements met, we can proceed.
Mongo Shell List Database Collection – Method 1
The most common method of viewing the available databases in a MongoDB Server is using the show dbs command.
Log-in into your Mongo Shell:
Once logged in, run the following command to show all the databases in the server:
The command should return all the databases in the server and their corresponding database size as shown in the following sample output:
config 72.00 KiB
customers 1.27 MiB
film 236.00 KiB
local 72.00 KiB
store 88.00 KiB
test 244.00 KiB
You can use the longer version of the command as:
Output:
Mongo Shell List Databases – listDatabases Command
Another technique that we can use to show the list of all the databases in the server is using the Mongo Shell admin command. The command returns the detailed information about the databases in the server as JSON object.
The command syntax is as shown in the following:
{
listDatabases: 1
}
)
The command should return an output as follows:
databases: [
{ name: 'admin', sizeOnDisk: Long("40960"), empty: false },
{ name: 'config', sizeOnDisk: Long("98304"), empty: false },
{ name: 'customers', sizeOnDisk: Long("1335296"), empty: false },
{ name: 'film', sizeOnDisk: Long("241664"), empty: false },
{ name: 'local', sizeOnDisk: Long("73728"), empty: false },
{ name: 'store', sizeOnDisk: Long("90112"), empty: false },
{ name: 'test', sizeOnDisk: Long("249856"), empty: false }
],
totalSize: Long("2129920"),
totalSizeMb: Long("2"),
ok: 1
}
The command returns the database name, the size taken on the disk, and whether or not the database contains the collections or not. Keep in mind that the previous command returns the databases in ascending order.
Mongo Shell List Databases – getMongo()
We can also use the getMongo() function to show the available databases in the server.
The command is as shown:
This should return the names of the databases in the server as follows:
Conclusion
In this post, we discussed the various methods and techniques of fetching the available databases in your MongoDB Server.
Thanks for reading!