revokePrivilegesFromRole
定义
revokePrivilegesFromRole
从运行命令的数据库中的用户定义角色中删除指定的权限。
提示
在
mongosh
中,此命令还可以通过db.revokePrivilegesFromRole()
辅助方法运行.辅助方法对
mongosh
用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。在不需要便利性或需要额外的返回字段的情况下,请使用数据库命令。
兼容性
此命令可在以下环境中托管的部署中使用
MongoDB Atlas:云中MongoDB部署的完全托管服务
重要
此命令在M0、M2和M5集群中不受支持。有关更多信息,请参阅不受支持的命令。
MongoDB 企业版:基于订阅、自助管理的 MongoDB 版本
MongoDB 社区版:开源、免费使用且可自助管理的 MongoDB 版本
语法
命令具有以下语法
db.runCommand( { revokePrivilegesFromRole: "<role>", privileges: [ { resource: { <resource> }, actions: [ "<action>", ... ] }, ... ], writeConcern: <write concern document>, comment: <any> } )
命令字段
命令包含以下字段
字段 | 类型 | 描述 |
---|---|---|
revokePrivilegesFromRole | string | 要从其中撤销特权的用户定义角色。 |
privileges | array | 要从角色中删除的权限数组。有关权限格式的更多信息,请参阅 privileges 。 |
writeConcern | document | 可选。操作的写入关注级别。有关写入关注级别的更多信息,请参阅 写入关注规范。 |
comment | 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" } } )