MongoDB is a popular NoSQL database that allows you to store diverse data types compactly using JSON documents. Its key-value structure makes it easy to search and retrieve values from MongoDB database. In this article, we will look at how to create user in MongoDB and assign user roles to them.
Create User in MongoDB
It is very easy to create user in MongoDB. Log into MongoDB.
$ sudo mongodb
Enter username & password when prompted to log in.
You can create a new user using CreateUser function, as shown below.
db.createUser( { user: "test_user", pwd: "test_password", roles:[{role: "userAdmin" , db:"admin"]})
In the above lines, we specify username (test_user) and password(test_password) for our new user.
Also read : How to Copy Files to Multiple Directories in Linux
Next, we need to specify the role for this user. We mention it as userAdmin that allows it to be the administrator of specified database. Here’s a list of all user roles available in Mongodb.
Finally, we specify the database name for which we are creating this user.
On executing the above command, MongoDB will output the following
Successfully added user: user: { "test_user", roles:[{role: "userAdmin" , db:"admin"] }
That’s it. As you can see it is very easy to add user in MongoDB and assign user roles.
Also read : How to Install MongoDB in Ubuntu 18.04/20.04
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.