In this tutorial, I will be discussing how you can list all databases, users, and collections in the mongo shell in easy ways.
This guide is meant for beginners and will walk through the simplest ways to achieve the same. So, let us get started.
We will be covering three topics on listing in the MongoDB Shell:
- List all databases in the mongo shell
- List all collections in the mongo shell
- List all users in the mongo shell
How to List All Databases in the MongoDB Shell?
Developers might work on several projects at once. You simply cannot have one database with different collections for different projects. It is always advisable you use separate databases for separate projects.
You might as well want to add new users with varying privileges and access controls in the future for your big project. At that time, having a single database isn’t a feasible solution.
Well, when you work on multiple projects, it calls for adding a dedicated database. This also calls for checking and navigating between those databases as and when you want to work in them. Let us see 3 methods on how we can list all databases in the mongo shell to be able to work in them.
Method 1
- Start your MongoDB server to automatically open your mongo shell. If your mongo shell does not open automatically, you can run the mongo.exe file in your system.
- Pass this command:
show dbs
OR
show databases
This will now list all databases you created. You can even see the default test, admin, config, and local databases listed here.
Method 2
Let’s see the second method to list all databases in the MongoDB shell.
To do so, pass this command:
db.adminCommand( { listDatabases: 1 , nameOnly : true} )
How to List All Collections in the MongoDB Shell?
There are many times you want to list all collections in the mongo shell and maybe navigate between them. Software and application development involve lots and lots of testing. You simply cannot write code and forget it. Most of the development involves testing.
While listing all collections is not just a part of testing, it is also done for reasons such as dummy databases, etc.
There are 4 different ways to list all collections in the mongo shell that I will demonstrate here. All have different purposes. To let you have a clear understanding, I will start with the simpler method and then proceed towards the detailed one.
Let us get started with learning how to list all collections in the MongoDB shell.
Method 1
- Start your MongoDB server to automatically open your mongo shell. If your mongo shell does not open automatically, you can run the mongo.exe file in your system.
- Navigate into any of your preferred database.
- Pass the command:
show collections
This command should now list all collections in your mongo shell. Make sure you have created some collections.
Method 2
Repeat steps 1 and 2 from the previous method and pass this command:
show tables
Method 3
Again, before you start, steps 1 and 2 are to be repeated if you have not navigated inside a database.
This method to list all collections will also output other details on the listed collections. Pass the command below to start:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
Users that have the privileges that allow access to the listCollections method will be able to see the names of all collections.
In case a user is not granted access to all collections in that database but some of them, then users will only be shown those collections.
Method 4
The final method demonstrates how you can list all collections based on a string query, in your MongoDB shell.
Pass this command to do so:
db.getCollectionNames().filter(function (CollectionName) { return /<Your Search Here>/.test(CollectionName) })
How to List All Users in the MongoDB Shell?
An enterprise or team that builds its products using MongoDB as the database management system, will have many users as participants to it. It also calls for giving them access to parts of the database where sensitive information resides.
As the application grows, more people may be added to the database for various roles. It is important to manage users from time to time and as and when they join or leave the organization.
A small part of it involves browsing through the names and other users. Let us learn 2 ways how we can list all users in the Mongo shell.
Method 1
- Start your MongoDB server to automatically open your mongo shell. If your mongo shell does not open automatically, you can run the mongo.exe file in your system.
- Pass this command to list all users in the mongo shell:
show users
This will now list all users in the MongoDB shell.
Method 2
Repeat the first method and pass this command:
db.getUsers();
This way you can list all users in the mongo shell. You can also see what roles they are assigned and what database do they belong to.
Read More: Top 8 MongoDB Hosting for Small Enterprises
Conclusion
This tutorial shows how you can list all users, databases, and collections in the Mongo shell.