Docs Menu
Docs Home
/
MongoDB Manual
/ / /

db.revokePrivilegesFromRole()

On this page

  • Definition
  • Compatibility
  • Behavior
  • Required Access
  • Example
db.revokePrivilegesFromRole(rolename, privileges, writeConcern)

Removes the specified privileges from the user-defined role on the database where the method runs.

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the revokePrivilegesFromRole command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

The db.revokePrivilegesFromRole() method has the following syntax:

db.revokePrivilegesFromRole(
"<rolename>",
[
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
{ <writeConcern> }
)

The db.revokePrivilegesFromRole() method takes the following arguments:

Parameter
Type
Description
rolename
string
The name of the user-defined role from which to revoke privileges.
privileges
array
An array of privileges to remove from the role. See privileges for more information on the format of the privileges.
writeConcern
document

Optional. The level of write concern for the operation. See Write Concern Specification.

This method is available in deployments hosted in the following environments:

Important

This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB

  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB

If run on a replica set, db.revokePrivilegesFromRole() is executed using "majority" write concern by default.

To revoke a privilege, the resource document pattern must match exactly the resource field of that privilege. The actions field can be a subset or match exactly.

For example, given the role accountRole in the products database with the following privilege that specifies the products database as the resource:

{
"resource" : {
"db" : "products",
"collection" : ""
},
"actions" : [
"find",
"update"
]
}

You cannot revoke find and/or update from just one collection in the products database. The following operations result in no change to the role:

use products
db.revokePrivilegesFromRole(
"accountRole",
[
{
resource : {
db : "products",
collection : "gadgets"
},
actions : [
"find",
"update"
]
}
]
)
db.revokePrivilegesFromRole(
"accountRole",
[
{
resource : {
db : "products",
collection : "gadgets"
},
actions : [
"find"
]
}
]
)

To revoke the "find" and/or the "update" action from the role accountRole, you must match the resource document exactly. For example, the following operation revokes just the "find" action from the existing privilege.

use products
db.revokePrivilegesFromRole(
"accountRole",
[
{
resource : {
db : "products",
collection : ""
},
actions : [
"find"
]
}
]
)

You must have the revokeRole action on the database a privilege targets in order to revoke that privilege. If the privilege targets multiple databases or the cluster resource, you must have the revokeRole action on the admin database.

The following operation removes multiple privileges from the associates role:

db.revokePrivilegesFromRole(
"associate",
[
{
resource: { db: "products", collection: "" },
actions: [ "createCollection", "createIndex", "find" ]
},
{
resource: { db: "products", collection: "orders" },
actions: [ "insert" ]
}
],
{ w: "majority" }
)

Back

db.grantPrivilegesToRole