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

revokeRolesFromUser(数据库命令)

revokeRolesFromUser

从角色所在的数据库中删除用户的一个或多个角色。

提示

In mongosh, this command can also be run through the db.revokeRolesFromUser() 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 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

该命令具有以下语法:

db.runCommand(
{
revokeRolesFromUser: "<user>",
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
writeConcern: { <write concern> },
comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

revokeRolesFromUser

字符串

要删除其中角色的用户。

roles

阵列

要从用户中删除的角色。

writeConcern

文档

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

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

roles 字段中,可以指定内置角色用户自定义角色

To specify a role that exists in the same database where revokeRolesFromUser runs, you can either specify the role with the name of the role:

"readWrite"

或者,可以使用文档指定角色,如下所示:

{ role: "<role>", db: "<database>" }

要指定存在于其他数据库中的角色,请使用文档指定该角色。

必须对数据库执行 revokeRole 动作才能撤消该数据库上的角色。

products数据库中的accountUser01用户具有以下角色:

"roles" : [
{ "role" : "assetsReader",
"db" : "assets"
},
{ "role" : "read",
"db" : "stock"
},
{ "role" : "readWrite",
"db" : "products"
}
]

The following revokeRolesFromUser command removes the two of the user's roles: the read role on the stock database and the readWrite role on the products database, which is also the database on which the command runs:

use products
db.runCommand( { revokeRolesFromUser: "accountUser01",
roles: [
{ role: "read", db: "stock" },
"readWrite"
],
writeConcern: { w: "majority" }
} )

products数据库中的用户accountUser01现在只剩下一个角色:

"roles" : [
{ "role" : "assetsReader",
"db" : "assets"
}
]