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.
1 |
sample> db.createRole( { role: "evaluator", privileges: [ { resource: { db: "sample", collection: "stu"}, actions: [ "find", "update" ] } ],roles: [ ] } ) |
➔ After this if the role got created then we get ACK like shown below
1 |
{ ok: 1 } |
➔ If the role that we create that already exist then we get the message as shown below
1 |
MongoServerError: Role "evaluator@sample" already exists |
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.
1 2 3 4 5 6 |
sample> show dbs admin 288.00 KiB config 84.00 KiB local 80.00 KiB sample 40.00 KiB sample2 40.00 KiB |
➔ Using the use command switch to the database that you want to check for the roles. switch to sample
➔ use sample.
1 2 |
admin> use sample switched to db sample |
➔ Then to get the roles use db.getRoles( ) method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
sample> db.getRoles() { roles: [ { _id: 'sample.readupdateremove', role: 'readupdateremove', db: 'sample', roles: [ { role: 'dbOwner', db: 'sample' } ], isBuiltin: false, inheritedRoles: [ { role: 'dbOwner', db: 'sample' } ] }, { _id: 'sample.updateandread', role: 'updateandread', db: 'sample', roles: [], isBuiltin: false, inheritedRoles: [] }, { _id: 'sample.updateremoveread', role: 'updateremoveread', db: 'sample', roles: [], isBuiltin: false, inheritedRoles: [] } ], ok: 1 } |
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.
1 2 3 |
use sample2 Then use the below command to create the user. sample2>db.createUser({user:"vamsi",pwd:"908055",roles:[{role:"readWri te",db:"sample2"}]}) |
➔ After this if the role got created then we get ACK like shown below
1 |
{ ok: 1 } |
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
1 |
sample>db.dropRole("evaluator") |
➔ When the role got dropped from the database we get ACK as shown
1 |
{ ok: 1 } |
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
Note: Please test scripts in Non Prod before trying in Production.