定义
revokePrivilegesFromRole删除运行命令的数据库上的用户定义角色的指定权限。
提示
In
mongosh, this command can also be run through thedb.revokePrivilegesFromRole()helper method.Helper methods are convenient for
mongoshusers, 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.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
重要
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { revokePrivilegesFromRole: "<role>", privileges: [ { resource: { <resource> }, actions: [ "<action>", ... ] }, ... ], writeConcern: <write concern document>, comment: <any> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 要撤销其中的权限的用户定义角色。 |
| 阵列 | 要从角色中删除的权限数组。 有关权限格式的更多信息,请参阅 |
| 文档 | |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
行为
要撤销权限,资源文档模式必须与该权限的resource字段完全匹配。 actions字段可以是子集,也可以完全匹配。
例如,假设products数据库中的accountRole角色具有将products数据库指定为资源的以下特权:
{ "resource" : { "db" : "products", "collection" : "" }, "actions" : [ "find", "update" ] }
您无法仅从products数据库中的一个集合中撤销find和/或update 。 以下操作不会导致角色发生变化:
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" ] } ] } )
要撤销角色accountRole中的"find"和/或"update"操作,必须与资源文档完全匹配。 例如,以下操作仅撤销现有特权中的"find"操作。
use products db.runCommand( { revokePrivilegesFromRole: "accountRole", privileges: [ { resource : { db : "products", collection : "" }, actions : [ "find" ] } ] } )
必需的访问权限
您必须对特权的目标数据库执行revokeRole操作才能撤销该特权。 如果特权针对多个数据库或cluster资源,则必须对admin数据库拥有revokeRole操作。
例子
以下操作会从products数据库中的associates角色中删除多个权限:
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" } } )