How to Revoke Privileges from any role in MongoDB?

Connect with

MongoDBThis article is about the admin job for mongoDB, in which I tried to explain, how to revoke privileges from any role to a user? In this post I tried to explain about user admin function i.e. db.revokePrivilegesFromRole.

1. Pre-requisite to revoke any role privileges from any Role

Assuming that following things are in place before proceeding to remove privileges to any role.

– MongoDB installed. if not you can visit another post How to install mongoDB?
– Auth enable to installed mongoDB. if not you can visit our another Post i.e. How to enable Auth in MongoDB using SCRAM-SHA-1 security mechanism?

2. Revoking privileges from any role

Follow below steps to remove previleges from a particular role

  1. logged into mongo shell
  2. switch to admin database
  3. build resource and revoke privilege by using db.revokePrivilegesFromRole () function
  4. get role and its privilege by using db.getRole() function

Grant Privileges to role:

db.grantPrivilegesToRole("trainingRole", [{rsource: {db:"", collection: "trainingColl"}, actions: ["insert", "update"]}]);

Get Role details:

db.getRole('trainingRole', {'showPrivileges': true});

mongo shell Console output

mongo
>use admin;
db.auth('userName', 'secret');
db.revokePrivilegesFromRole('trainingRole', [{resource: {db:"", collection: "trainingColl"}, actions: ["update"]}])

db.getRole('trainingRole', {'showPrivileges': true});

3. Quiz on revoking privileges from role

One quiz to know , whether you remember the name of function to remove privileges from any role or not.

Question. Which of the following is the correct function to revoke a privilege from a role?
Choose the best answer:
a. db.revokePrivilegeFromRole
b. db.revokePrivilegesFromRole
c. db.removePrivilegeFromRole
d. db.removePrivilegesFromRole

Answer:
The correct function to revoke a privilege from a role is db.revokePrivilegesFromRole.


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *