Roles In MongoDB Part – 2

Share via:

Roles In MongoDB Part – 2

User-Defined roles:
MongoDB provides many built-in roles to manage the access to the database.but when the built-in roles did not meet your requirement privileges then you can go for creating your own roles.
Creation of user-Defined role:
To create a role in Mongodb we use the db.createRole( ) method, with two parameters.
Syntax : db.createRole(role,writeconcern)
Where role parameter is a document type which includes the role name,privileges, Authentication restrictions ,roles.
Writeconcern is a document type which is optional that includes wtimeout.
Example :
To create a role named evaluator on a database named “sample” and a collection named “stu”.
➔ First switch to sample database using the use command like
Ue sample
➔ Then create the role with privileges and resources, like shown below.

➔ After this if the role got created then we get ACK like shown below

➔ If the role that we create that already exist then we get the message as shown below

To Get all the roles:
To get all the views in a particular database initially you have to switch to that database using the use command in a MongoDB instance.
Example:
➔ By using the show dbs command view all the databases in the MongoDB instance.

➔ Using the use command switch to the database that you want to check for the roles. switch to sample
➔ use sample.

➔ Then to get the roles use db.getRoles( ) method.

Creation of user with user-Defined role:
To create a user with a role in Mongodb, use the db.createUser( ) method.
Syntax: db.createUser( )
Example:
➔ To create a user named vamsi with the required password role named evaluator on a database named “sample2”.
➔ Switch to sample2 database.

➔ After this if the role got created then we get ACK like shown below

Drop the role in the database:
To remove or drop the role which is user-Defined role, use the db.dropRole( ) method.
Syntax: db.dropRole(“role _name”) Example:
➔ To drop the user called evaluator

➔ When the role got dropped from the database we get ACK as shown

 

Author    : Teja
LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment