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()”
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:
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“.
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”
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:
Firstly, us the following command to get the number of users present on your current database:
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:
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:
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:
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.