MongoDB

How do I list users in MongoDB

MongoDB is a NoSQL type of database that supports a variety of commands to modify and access any document. With the help of these commands, developers can check or list down the users of a database in MongoDB. This seems normal but if there is a huge amount of crucial data then it is important to have a check of users accessing it to avoid any mishap with the data. In this way, organizations can keep data in safe hands or allow only trusted people to access it.

In this article, we will list down several commands and their associated examples to list down the users that are currently allowed to use that database.

How users can be listed in MongoDB

There are several commands that Mongo shell supports, and these commands are used specifically for getting the users of a MongoDB shell. The following commands and syntaxes can be followed in this regard.

Syntax to use “getUsers()”

db.getUsers(<options>)

Note: The field (<options>) is optional; that means you can get the list of users without specifying options. Although they can get you to the result that you demand.

The <options> in the above field can be one of the following parameters:

showCredentials: <Boolean>

You can get the password hash of any user by using the “showCredentials:<Boolean>” in “db.getUsers(<>). “This option accepts <Boolean> value, that may be either true or false. By default, the Boolean value is “False“.

filter: <document>

The filter options may be used to get the output that matches the document/condition specified in the filter option.

Syntax to use “show users”

show users

Both syntaxes can be used to get the list of users.

How to list users in MongoDB

In this section, we will show you to list down the users on your MongoDB database.

Example 1: Using db.getUser() to list down the users

We are using here a database named “linuxhint“. You can use any database(must be present on your MongoDB) and can connect to it as we did in the below-mentioned command:

> use linuxhint

Text, logo Description automatically generated with medium confidence

Firstly, us the following command to get the number of users present on your current database:

> db.getUsers()

Text Description automatically generated

Two users have access to the “linuxhint” database. The “_id” field in the above output is created by joining the database name with the name of the “user“.

Example 2: Using getUsers() to get password hash

As mentioned earlier, “getUsers()” method of MongoDB supports two options. Let’s apply the getUsers() method on the same database (linuxhint) and set the “showCredentials” value to “true“. The following command will return password hash of all users:

> db.getUsers({"showCredentials: true"})

Text Description automatically generated

Text Description automatically generated

Example 3: Getting the number of users

One can get the number (a numeric value) of users on your database and in this way, they can skip/ignore the detailed information of any user. So, to get the exact number of users; you can use the following command:

> db.getUsers().length

A screenshot of a video game Description automatically generated with medium confidence

Example 4: Using show users to get all users

The “show users” method in MongoDB also displays the number of users that can access a specific database. The following command has listed down the users on the “test” database:

> show users

Conclusion

The methods and commands supported by MongoDB can be used to process data inside databases. The processing can be of any type, like sorting the data, updating the documents, or getting the list of database users. This article lists down the possible ways to get the user’s details in the MongoDB database. We have explained two ways to get the list of users present. Additionally, you can also get a number that represents the total user count. By following this guide, the database administrators can observe the users working on a database as well as the roles assigned to them can also be viewed.

About the author

Adnan Shabbir