How to List all Users in the Mongo Shell
In MongoDB, user management is an essential aspect of database administration, allowing administrators to control access and permissions for different users. The Mongo Shell provides a powerful interface to interact with MongoDB including managing users.
In this article, we’ll explore how to list all users in the Mongo Shell by covering concepts, examples to understand the process effectively.
How to List all Users in the Mongo Shell?
In MongoDB, effective user management is essential for maintaining database security and controlling access to resources. One of the key tasks in user management is listing all users configured in the MongoDB database.
- Using Show Command
- Using getUsers() Command
Prerequisites
Before further learning let make sure that we have basic understanding of following:
- MongoDB installed on your system
- Access to the Mongo Shell
- Basic understanding of MongoDB concepts
Connecting to MongoDB
Start by launching the MongoDB shell (mongo) and connecting to the desired database.
mongo
If your MongoDB server is running on a different host or port, you can specify the connection string.
mongo --host <hostname> --port <port>
Listing All Users
To list all users configured for a specific database in MongoDB, use the show users command within the MongoDB shell. This command displays a list of all users and their associated roles for the current database.
use mydatabase
show users
Replace mydatabase with the name of the database for which you want to list the users. The use command switches to the specified database, and show users lists all users configured for that database.
1. Using Show Command
Let’s walk through an example of listing users in a sample MongoDB database named mydatabase.
Connect to MongoDB and switch to the mydatabase database:
mongo
> use mydatabase
List all users configured for the mydatabase database:
> show users
Sample Output
{
"_id" : "mydatabase.user",
"userId" : UUID("8cf6e0b8-1e05-4d34-884b-54dcd6b0b1cf"),
"user" : "user1",
"db" : "mydatabase",
"roles" : [
{
"role" : "readWrite",
"db" : "mydatabase"
}
],
"mechanisms" : [
"SCRAM-SHA-256"
]
}
In this example:
- The show users command displays detailed information about each user configured for the mydatabase database.
- Each user entry includes:
- _id: Unique identifier for the user.
- user: Username.
- db: Database associated with the user.
- roles: Array of roles assigned to the user within the specified database.
- mechanisms: Authentication mechanisms enabled for the user.
2. Using getUsers() Command
Another way to list users in MongoDB is by using the getUsers()
method. This method retrieves a list of users from the current database.
use admin
db.getUsers()
Sample Output:
[
{
"_id" : "mydatabase.user",
"userId" : UUID("8cf6e0b8-1e05-4d34-884b-54dcd6b0b1cf"),
"user" : "user1",
"db" : "mydatabase",
"roles" : [
{
"role" : "readWrite",
"db" : "mydatabase"
}
],
"mechanisms" : [
"SCRAM-SHA-256"
]
}
]
In this example, the db.getUsers()
method retrieves a list of users from the admin
database.
Handling User Authentication
- When accessing the MongoDB shell and listing users, ensure that we have appropriate privileges to view user information.
- Depending on our MongoDB deployment, we may need administrative or read access to perform user-related operations.
Additional Commands for User Management
In addition to listing users, MongoDB provides other commands for user management within the shell:
- show users: Display all users for the current database.
- show roles: Display all built-in roles available in MongoDB.
Conclusion
Overall, Listing all users in the MongoDB shell is essential for understanding the current database’s user configuration and permissions. By understanding commands like show users and getUsers() within the MongoDB shell, you can view detailed information about each user and their assigned roles. In this article, we covered the process of listing users in the MongoDB shell, demonstrated with examples and sample outputs.