revokePrivilegesFromRole (original) (raw)

revokePrivilegesFromRole

Removes the specified privileges from the user-defined role on the database where the command is run.

Tip

In mongosh, this command can also be run through the db.revokePrivilegesFromRole() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

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

Important

This command is not supported in M0, M2, M5, and Flex clusters. For more information, see Unsupported Commands.

The command has the following syntax:


db.runCommand(

   {

     revokePrivilegesFromRole: "<role>",

     privileges: [

       { resource: { <resource> }, actions: [ "<action>", ... ] },

       ...

     ],

     writeConcern: <write concern document>,

     comment: <any>

   }

)

The command takes the following fields:

Field Type Description
revokePrivilegesFromRole string The user-defined role to revoke privileges from.
privileges array An array of privileges to remove from the role. Seeprivileges for more information on the format of the privileges.
writeConcern document Optional. The level of write concern for the operation. See Write Concern Specification.
comment any Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:mongod log messages, in theattr.command.cursor.comment field.Database profiler output, in the command.comment field.currentOp output, in the command.comment field.A comment can be any valid BSON type(string, integer, object, array, etc).

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

For example, consider the role accountRole in the productsdatabase with the following privilege that specifies the productsdatabase 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.runCommand(

    {

      revokePrivilegesFromRole: "accountRole",

      privileges:

        [

          {

            resource : {

                db : "products",

                collection : "gadgets"

            },

            actions : [

                "find",

                "update"

            ]

          }

        ]

    }

)

db.runCommand(

    {

      revokePrivilegesFromRole: "accountRole",

      privileges:

        [

          {

            resource : {

                db : "products",

                collection : "gadgets"

            },

            actions : [

                "find"

            ]

          }

        ]

    }

)

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


use products

db.runCommand(

    {

      revokePrivilegesFromRole: "accountRole",

      privileges:

        [

          {

            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 thecluster resource, you must have the revokeRole action on the admin database.

The following operation removes multiple privileges from theassociates role in the products database:


use products

db.runCommand(

   {

     revokePrivilegesFromRole: "associate",

     privileges:

      [

        {

          resource: { db: "products", collection: "" },

          actions: [ "createCollection", "createIndex", "find" ]

        },

        {

          resource: { db: "products", collection: "orders" },

          actions: [ "insert" ]

        }

      ],

     writeConcern: { w: "majority" }

   }

)