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

updateRole(数据库命令)

updateRole

Updates a user-defined role. The updateRole command must run on the role's database.

提示

In mongosh, this command can also be run through the db.updateRole() 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.

对字段的更新会完全替换先前字段的值。 要授予或删除角色或权限而不替换所有值,请使用以下一个或多个命令:

警告

对 privileges 或 roles 数组的更新将完全替换先前数组的值。

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

重要

This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

要更新角色,您必须提供privileges数组、 roles数组或两者兼而有之。

该命令采用以下语法:

db.runCommand(
{
updateRole: "<role>",
privileges:
[
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
roles:
[
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions:
[
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>", ...]
},
...
]
writeConcern: <write concern document>,
comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

updateRole

字符串

要更新的用户定义角色的名称。

privileges

阵列

可选。如果未指定 roles 数组,则为必填项。授予角色的权限。privileges数组的更新会覆盖之前数组的值。有关指定权限的语法,请参阅 privileges 数组。

roles

阵列

可选。如果未指定 privileges 数组,则为必填项。此角色从中继承权限的角色。roles 数组的更新会覆盖之前数组的值。

authenticationRestrictions

阵列

可选。

服务器对角色实施的身份验证限制。指定允许授予此角色的用户连接到和/或可以从中进行连接的 IP 地址和 CIDR 范围列表。

writeConcern

文档

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

comment

any

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

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

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

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

"readWrite"

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

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

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

authenticationRestrictions 文档只能包含以下字段。如果 authenticationRestrictions 文档包含无法识别的字段,服务器会引发错误:

字段名称
值
说明

clientSource

IP 地址和/或 CIDR 范围的数组

如果存在,则在对用户进行身份验证时,服务器会验证客户端的 IP 地址是否在给定列表中或属于列表中的 CIDR 范围。如果客户端的 IP 地址不存在,服务器不会对用户进行身份验证。

serverAddress

IP 地址和/或 CIDR 范围的数组

客户端可以连接的 IP 地址或 CIDR 范围列表。如果存在,服务器将验证客户端的连接是否已通过给定列表中的 IP 地址接受。如果通过无法识别的 IP 地址接受连接,服务器不会对用户进行身份验证。

重要

如果用户继承的多个角色具有不兼容的身份验证限制,则该用户将不可用。

例如,如果用户继承了一个角色(其中 clientSource 字段为 ["198.51.100.0"])和另一个角色(其中 clientSource 字段为 ["203.0.113.0"]),则服务器无法对该用户进行身份验证。

有关MongoDB中身份身份验证的更多信息,请参阅自托管部署上的身份验证。

角色的权限适用于创建该角色的数据库。该角色可以从其数据库中的其他角色继承权限。在 admin 数据库上创建的角色可以包含适用于所有数据库或集群的权限,并且可以从其他数据库中的角色继承权限。

必须对所有数据库执行 revokeRole 操作才能更新角色。

您必须对 roles 数组中的每个角色的数据库执行 grantRole 操作才能更新该数组。

您必须对 privileges 数组中的每个权限的数据库执行 grantRole 操作才能更新该数组。如果权限的资源跨越数据库,则在 admin 数据库上必须有 grantRole。如果权限为以下任一权限,则该权限将跨越数据库:

  • 所有数据库中的一个集合

  • 所有集合和所有数据库

  • cluster 资源

您必须对目标角色的数据库执行 setAuthenticationRestriction 操作,才能更新角色的 authenticationRestrictions 文档。

The following is an example of the updateRole command that updates the myClusterwideAdmin role on the admin database. While the privileges and the roles arrays are both optional, at least one of the two is required:

db.adminCommand(
{
updateRole: "myClusterwideAdmin",
privileges:
[
{
resource: { db: "", collection: "" },
actions: [ "find" , "update", "insert", "remove" ]
}
],
roles:
[
{ role: "dbAdminAnyDatabase", db: "admin" }
],
writeConcern: { w: "majority" }
}
)

要查看某个角色的权限,请使用 rolesInfo 命令。