How to Revoke Privileges from any role in MongoDB?

Connect with

Revoke privileges in MongoDBThis article is about revoke privileges in MongoDB using db.revokePrivilegesFromRole and grant privileges in MongoDB using db.grantPrivilegesToRole.

1. Overview of grant privileges, revoke previleges and get role

In this article I explain about:

  • how to revoke privileges in mongoDB from any role to a user using db.revokePrivilegesFromRole
    function
  • how to grant privileges in mongoDB to any role to a user using db.grantPrivilegesToRole function
  • how to get role and its privilege by using db.getRole() function

2. Pre-requisite to revoke privileges in MongoDB from any Role

Assuming that the 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?

3. Revoking privileges in MongoDB using db.revokePrivilegesFromRole

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

3. Grant Privileges in MongoDB to role: db.grantPrivilegesToRole()

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

4. Get Role using db.getRole

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

5. 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});

6. Quiz on revoking privileges from role

One quiz to know, whether you remember the name of the 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.

7. Reference

You can visit docs.mongodb.com for more details.

Thanks for visiting this post for revoke privileges in mongoDB using db.revokePrivilegesFromRole and grant privileges in MongoDB using db.grantPrivilegesToRole. You can also visit MongoDB Tutorial Listing page for more articles on MongoDB document-oriented database.
Happy Learning 🙂


Connect with

Leave a Comment

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