对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

revokePrivilegesFromRole(数据库命令)

revokePrivilegesFromRole

删除运行命令的数据库上的用户定义角色的指定权限。

提示

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.

此命令可用于以下环境中托管的部署:

  • 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.

该命令具有以下语法:

db.runCommand(
{
revokePrivilegesFromRole: "<role>",
privileges: [
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
writeConcern: <write concern document>,
comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

revokePrivilegesFromRole

字符串

要撤销其中的权限的用户定义角色。

privileges

阵列

要从角色中删除的权限数组。 有关权限格式的更多信息,请参阅privileges 。

writeConcern

文档

可选。操作的写关注级别。请参阅写关注规范。

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" }
}
)