MongoDB – List all Collections, Database using MongoShell

List all Collections mongodb

Today in the article, we shall see how to list all Collections using MongoShell from the MongoDB database. We shall also see how to get a list of databases or switch to different databases using mongo shell commands.

Today in this article, we will cover below aspects,

We shall mainly use Mongo Shell CLI commands to perform this.

Login to the MongoDB database

Using mongo shell, please log in to the database using a connection string,

mongo <MongoDB connection string>

Your connection string not necessarily might be having database names.

If you have it, then you will get connected to the required database directly using the above command.

Get the list of all databases in MongoDB

Using the mongo shell, please run the below command to list all the databases available.

Command:

show dbs

Example

List All Collection using MongoShell

As explained show dbs commands will give a list of all databases available for the given cluster.

List all collections using MongoShell

Using the mongo shell, please run the below command to list all the collections available in the given database.

First, you should log in to the required database using the connection string directly or using the below use database command.

Command:

use BooksstoreDb

Example:

List All database using MongoShell

So above we switched to the required database i.e BooksstoreDb. Now please run any of the below commands to get the details on available collections.

Command:

show TABLES

OR

show collections

Using either of the above commands produce the same results as below,

Example:

MongoDB List all Collections using MongoShell

As explained show tables/collections commands will give the list of all collections available in the given mongo database.

That’s all.

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

References:



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *