Database Manual / Reference / Database Commands / Role Management

updateRole (database command数据库命令)

Definition定义

updateRole

Updates a user-defined role. The updateRole command must run on the role's database.更新用户定义的角色updateRole命令必须在角色的数据库上运行。

Tip

In mongosh, this command can also be run through the db.updateRole() helper method.mongosh中,此命令也可以通过db.updateRole()辅助方法运行。

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.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。

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 commands:字段的更新将完全替换前一个字段的值。要授予或删除角色或权限而不替换所有值,请使用以下一个或多个命令:

Warning

An update to the privileges or roles array completely replaces the previous array's values.privilegesroles数组的更新将完全替换前一个数组的值。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Important

This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

Syntax语法

To update a role, you must provide the privileges array, roles array, or both.要更新角色,您必须提供privileges数组、roles数组或两者都提供。

The command uses the following syntax:该命令使用以下语法:

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

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
updateRolestring字符串The name of the user-defined role role to update.要更新的用户定义角色的名称。
privilegesarray数组Optional. 可选。Required if you do not specify roles array. 如果不指定roles数组,则为必填项。The privileges to grant the role. An update to the privileges array overrides the previous array's values.授予角色的权限。privileges数组的更新会覆盖前一个数组的值。 For the syntax for specifying a privilege, see the privileges array.有关指定权限的语法,请参阅privileges数组。
rolesarray数组Optional. 可选。Required if you do not specify privileges array. 如果不指定privileges数组,则为必填项。The roles from which this role inherits privileges. An update to the roles array overrides the previous array's values.此角色继承权限的角色。对roles数组的更新会覆盖前一个数组的值。
authenticationRestrictionsarray数组

Optional.可选。

The authentication restrictions the server enforces on the role. Specifies a list of IP addresses and CIDR ranges users granted this role are allowed to connect to and/or which they can connect from.服务器对角色实施的身份验证限制。指定允许授予此角色的用户连接和/或可以从中连接的IP地址和CIDR范围的列表。

writeConcerndocument文档Optional. 可选。The level of write concern for the operation. 操作的写入关注级别。See Write Concern Specification.请参阅写入关注规范
commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

Roles角色

In the roles field, you can specify both built-in roles and user-defined roles.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:要指定存在于运行updateRole的同一数据库中的角色,您可以使用角色的名称指定角色:

"readWrite"

Or you can specify the role with a document, as in:或者,您可以在文档中指定角色,如:

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

To specify a role that exists in a different database, specify the role with a document.若要指定存在于其他数据库中的角色,请使用文档指定该角色。

Authentication Restrictions身份验证限制

The authenticationRestrictions document can contain only the following fields. The server throws an error if the authenticationRestrictions document contains an unrecognized field:authenticationRestrictions文档只能包含以下字段。如果authenticationRestrictions文档包含无法识别的字段,服务器将抛出错误:

Field Name字段名称ValueDescription描述
clientSourceArray of IP addresses and/or CIDR rangesIP地址和/或CIDR范围数组If present, when authenticating a user, the server verifies that the client's IP address is either in the given list or belongs to a CIDR range in the list. If the client's IP address is not present, the server does not authenticate the user.如果存在,在对用户进行身份验证时,服务器会验证客户端的IP地址是否在给定的列表中,或者是否属于列表中的CIDR范围。如果客户端的IP地址不存在,则服务器不会对用户进行身份验证。
serverAddressArray of IP addresses and/or CIDR rangesIP地址和/或CIDR范围数组A list of IP addresses or CIDR ranges to which the client can connect. If present, the server will verify that the client's connection was accepted via an IP address in the given list. If the connection was accepted via an unrecognized IP address, the server does not authenticate the user.客户端可以连接的IP地址或CIDR范围列表。如果存在,服务器将验证客户端的连接是否通过给定列表中的IP地址被接受。如果通过无法识别的IP地址接受连接,则服务器不会对用户进行身份验证。

Important

If a user inherits multiple roles with incompatible authentication restrictions, that user becomes unusable.如果用户继承了多个身份验证限制不兼容的角色,则该用户将无法使用。

For example, if a user inherits one role in which the clientSource field is ["198.51.100.0"] and another role in which the clientSource field is ["203.0.113.0"] the server is unable to authenticate the user.例如,如果用户继承了一个角色,其中clientSource字段为["198.51.100.0"],另一个角色的clientSource字段是["203.0.113.0"],则服务器无法对用户进行身份验证。

For more information on authentication in MongoDB, see Authentication on Self-Managed Deployments.有关MongoDB中身份验证的更多信息,请参阅自我管理部署的身份验证

Behavior行为

A role's privileges apply to the database where the role is created. The role can inherit privileges from other roles in its database. 角色的权限适用于创建该角色的数据库。该角色可以继承其数据库中其他角色的权限。A role created on the admin database can include privileges that apply to all databases or to the cluster and can inherit privileges from roles in other databases.admin数据库上创建的角色可以包括应用于所有数据库或集群的权限,并且可以继承其他数据库中角色的权限。

Required Access所需访问权限

You must have the revokeRole action on all databases in order to update a role.您必须对所有数据库执行revokeRole操作才能更新角色。

You must have the grantRole action on the database of each role in the roles array to update the array.您必须对角色数组中每个角色的数据库执行grantRole操作才能更新数组。

You must have the grantRole action on the database of each privilege in the privileges array to update the array. 您必须对privileges数组中每个权限的数据库执行grantRole操作才能更新数组。If a privilege's resource spans databases, you must have grantRole on the admin database. A privilege spans databases if the privilege is any of the following:如果权限的资源跨越数据库,则必须在admin数据库上具有grantRole。如果权限是以下任何一种,则该权限跨越数据库:

  • a collection in all databases所有数据库中的集合
  • all collections and all database所有集合和所有数据库
  • the cluster resourcecluster资源

You must have the setAuthenticationRestriction action on the database of the target role to update a role's authenticationRestrictions document.您必须对目标角色的数据库执行setAuthenticationRestriction操作,才能更新角色的authenticationRestrictions文档。

Example示例

The following is an example of the updateRole command that updates the myClusterwideAdmin role on the admin database. 以下是updateRole命令的示例,该命令更新管理数据库上的myClusterwideAdmin角色。While the privileges and the roles arrays are both optional, at least one of the two is required:虽然privilegesroles数组都是可选的,但至少需要其中之一:

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

To view a role's privileges, use the rolesInfo command.要查看角色的权限,请使用rolesInfo命令。