How To list All Databases In The Mongo Shell?

In this tutorial, we will learn How To list All Databases In The Mongo Shell we can list all the databases in MongoDB in three ways one by the Show database command, Second could be the admin command with the simple show, and the admin command with detailed information of the database.

 

Databases

 

Ways To list All Databases

In MongoDB Database is unstructured which means data is not stored in any table raw and columns as the data in the MongoDB database is stored in the form of documents and in documents the data is stored as collections that do not have any specific orientation.

Which simply means storing the data of one record in one place as key- value pairs and does not require any pre defined structure

There are three ways to list all the databases present and we will look at all three one by one.

With the Show Database command

It is will enlist all the databases to present there along with their size and most of the time this method is used to letting the list of databases learn more and understand the execution following the example given below.

show dbs

Note that there will always be a database name local which you did not create it is an in- build database that specific to a single node in MongoDB.

Let’s look at other methods

Admin Commands

Admin command can be used in two different ways and we will look at how it works.

Display Without SIze

We can also display the list of databases without the listed size of databases where the list will contain only the names of the database not the size of the databases. Here is a line of code that need to be executed for the same to do so which is given below.

db.adminCommand({listDatabases: 1})

It will display the list without the size.

Display With SIze

There is a way to display the list of databases with size also using the admin command too and to do so there is a line of command which need to be executed and to do so follow the example given below.

db.adminCommand({listDatabases: 1}).databases.map(function(db) { return db.name; })

 

To learn more about How To list All Databases In The Mongo Shell visit Display all MongoDB databases present there.

And also visit beta python for programing Solutions to get the solutions of all the programing languages including java, and Python.

Leave a Comment

%d bloggers like this: