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

db.revokePrivilegesFromRole()(mongosh方法)

db.revokePrivilegesFromRole(rolename, privileges, writeConcern)

在该方法运行的数据库上,删除用户定义角色的指定特权。

重要

mongosh 方法

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

有关数据库命令,请参阅 revokePrivilegesFromRole 命令。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

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
类型
说明

rolename

字符串

要撤销权限的用户定义角色的名称。

privileges

阵列

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

writeConcern

文档

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

此方法可用于以下环境中托管的部署:

重要

MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

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

要撤销权限,资源文档模式必须与该权限的resource字段完全匹配。 actions字段可以是子集,也可以完全匹配。

例如,假设products数据库中的accountRole角色具有将products数据库指定为资源的以下特权:

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

无法仅从products数据库中的一个集合中撤销find和/或update 。 以下操作不会导致角色发生变化:

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

要撤销角色accountRole中的"find"和/或"update"操作,必须与资源文档完全匹配。 例如,以下操作仅撤销现有特权中的"find"操作。

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

当您指定privileges 大量时,可以指定要应用数据库中的多个集合或整个数据库的权限。

以下语法指定对 products数据库中多个集合的权限。

privileges: [
{
resource: { db: 'products', collection: 'coll1' },
actions: [ 'bypassDocumentValidation' ]
},
{
resource: { db: 'products', collection: 'coll2' },
actions: [ 'bypassDocumentValidation' ]
}
]

以下语法指定对 products数据库中所有集合的权限。

privileges: [
{
resource: { db: 'products', collection: '' },
actions: [ 'bypassDocumentValidation' ]
}
]

您必须对特权的目标数据库执行revokeRole操作才能撤销该特权。 如果特权针对多个数据库或cluster资源,则必须对admin数据库拥有revokeRole操作。

以下操作会删除associates角色的多个特权:

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