List All Users, Collections, and Databases in the Mongo Shell

MongoDB lets us create multiple users. A user can create multiple databases. A database can have multiple collections. A collection can have multiple documents. We already know how to get documents from a collection in Mongo Shell. It can be done by using find(), findOne(), like method. 

However, as a beginner, we don’t usually log lists of users, databases or collections, but it is also important to know how to get these data. It involves fundamental queries and methods which are important to be aware of.

In this article, we will not only simply learn to list users, databases, and collections but we will understand multiple ways of doing the same so that you will also get introduced to some new methods in MongoDB. 

For simplicity we have divided this article into three sections below:

  • List all databases
  • List all collections
  • List all users

Now without any further discussion let’s start with the listing database first.

Also Read: How to Connect MongoDB on Localhost 27017

List All Databases in the Mongo 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 wasn’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. Below are 2 methods to list all databases in MongoDB shell.

Method 1:

Start your MongoDB server to automatically open the Mongo shell. If the shell does not open automatically, you can run the mongo.exe file in your system.

Execute any of the below commands in the shell:

show dbs

OR 

show databases

These commands will list all the MongoDB databases you have created. You can even see the default test, admin, config, and local databases listed here.

Method 2:

In this method, we will take the advance of adminCommand() for listing the database. The adminCommand() method can run administrative commands.

Execute the below command in the shell:

db.adminCommand( { listDatabases: 1 , nameOnly : true} )

Here ‘listDatabase’ is specified as 1 to get the database list, and ‘nameOnly’ is true indicating that only database names we want to log on the shell, this is done to avoid getting detailed information.

List All Collections in the Mongo Shell

There are many times you want to list all collections in the Mongo shell and maybe navigate between them for testing. 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 multiple different ways to list all collections in the Mongo shell that we will be demonstrating. All have different purposes. To let you have a clear understanding, let’s start with a simpler method and then proceed towards the detailed one.

Method 1:

Start your MongoDB server to automatically open your Mongo shell. If your shell does not open automatically, you can run the mongo.exe file in your system. Then navigate into any current database.

Execute the below command to list all collections in the shell:

show collections

This command should now list all collections in your shell. Make sure you have created some collections.

Method 2:

Repeat steps 1 and 2 from the previous method and execute the below 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 of the listed collections.

Execute the below command in the shell:

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 to list all collections is to use the string query in the Mongo shell.

Execute the below command in the shell:

db.getCollectionNames().filter(function (CollectionName) { return /<Your Search Here>/.test(CollectionName) })

List All Users in the Mongo 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. Let’s see 2 methods to list users in the Mongo shell.

Method 1:

Again, first, start the MongoDB server to automatically open the Mongo shell. If the shell does not open automatically, you can run the mongo.exe file in your system.

Execute the below command to list all users in the shell:

show users

This will now give a list of all users exist. You may not have many users by default, try creating a new user for demonstration using db.createUser() method.

Method 2:

Another way to list all users is using the db.getUsers() method.

Execute the below command in the shell:

db.getUsers();

This way you can also get a list of all users. You can also see what roles they are assigned and what database they belong to.

Also, among users you got as a result, you can pass a user’s name into the db.getUser() method to get more in-depth information regarding it.

Conclusion

In this guide, we have seen many methods with syntax & examples for listing MongoDB users, collections and databases using Mongo shell. Among them, let us now filter which is easier for beginners. To list databases, “show dbs” is the fastest and easiest way, to list collections, the best way is to use the “show collections” command, and to list users in MongoDB, “show users” is highly preferred. Other methods are given in this article to help you in the future by familiarising you with them or when you don’t want to use show commands. Hope you have enjoyed reading the content and learned something new.

Read More: Random Password Generator in NodeJS

References

Aneesha S
Aneesha S
Articles: 171