Docs 菜单

db.updateRole()

db.updateRole( rolename, update, writeConcern )

Updates a user-defined role. The db.updateRole() method must run on the role's database.

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是数据库命令或特定语言驱动程序(例如 Node.js)的相关文档。

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

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

An update to a field completely replaces the previous field's values. To grant or remove roles or privileges without replacing all values, use one or more of the following methods:

警告

An update to the privileges or roles array completely replaces the previous array's values.

db.updateRole() 方法使用以下语法:

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

db.updateRole() 方法接受以下参数:

Parameter
类型
说明

rolename

字符串

The name of the user-defined role to update.

update

文档

A document containing the replacement data for the role. This data completely replaces the corresponding data for the role.

writeConcern

文档

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

The update document specifies the fields to update and the new values. Each field in the update document is optional, but the document must include at least one field. The update document has the following fields:

字段
类型
说明

privileges

阵列

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

roles

阵列

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

authenticationRestrictions

阵列

可选。

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

版本 3.6 中的新增功能

db.updateRole() 方法封装了 updateRole 命令。

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

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

要指定运行 db.updateRole() 的同一数据库中存在的角色,可以使用角色名称指定角色:

"readWrite"

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

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

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

版本 3.6 中的新增功能

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 中身份验证的更多信息,请参阅身份验证

如果在副本集上运行,则默认情况下使用 "majority" 写关注执行 db.updateRole()

除在 admin 数据库中创建的角色之外,角色只能包含会应用于其数据库的特权,且只能从其数据库中的其他角色来继承。

admin 数据库中创建的角色可包含会应用于 admin 数据库、其他数据库或集群资源的特权,且可从其他数据库和 admin 数据库中的角色来继承。

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

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

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

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

  • 所有集合和所有数据库

  • cluster 资源

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

The following db.updateRole() method replaces the privileges and the roles for the inventoryControl role that exists in the products database. The method runs on the database that contains inventoryControl:

use products
db.updateRole(
"inventoryControl",
{
privileges:
[
{
resource: { db:"products", collection:"clothing" },
actions: [ "update", "createCollection", "createIndex"]
}
],
roles:
[
{
role: "read",
db: "products"
}
]
},
{ w:"majority" }
)

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