Docs HomeMongoDB Manual

db.updateRole()

Definition定义

db.updateRole( rolename, update, writeConcern )

Updates a user-defined role. 更新用户定义的角色The db.updateRole() method must run on the role's database.db.updateRole()方法必须在角色的数据库上运行。

Important

mongosh Method

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

For the database command, see the updateRole command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

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:要在不替换所有值的情况下授予或删除角色或权限,请使用以下一种或多种方法:

Warning

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

The db.updateRole() method uses the following syntax: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> }
)

The db.updateRole() method accepts the following arguments:db.updateRole()方法接受以下参数:

Parameter参数Type类型Description描述
rolenamestringThe name of the user-defined role to update.要更新的用户定义角色的名称。
updatedocumentA document containing the replacement data for the role. 包含角色的替换数据的文档。This data completely replaces the corresponding data for the role.此数据完全替换角色的相应数据。
writeConcerndocumentOptional.可选的。The level of write concern for the operation. 操作的写入关注级别。See Write Concern Specification.请参阅写入关注规范

The update document specifies the fields to update and the new values. update文档指定要更新的字段和新值。Each field in the update document is optional, but the document must include at least one field. The update document has the following fields:更新文档中的每个字段都是可选的,但文档必须至少包含一个字段。update文档包含以下字段:

Field字段Type类型Description描述
privilegesarrayOptional.可选的。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. 权限数组的更新将覆盖上一个数组的值。For the syntax for specifying a privilege, see the privileges array.有关指定权限的语法,请参阅privileges数组。
rolesarrayOptional.可选的。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.对角色数组的更新将覆盖上一个数组的值。
authenticationRestrictionsarrayOptional.可选的。
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范围的列表。

The db.updateRole() method wraps the updateRole command.db.updateRole()方法包装updateRole命令。

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 db.updateRole() runs, you can either specify the role with the name of the role:要指定运行db.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. 如果存在,则在对用户进行身份验证时,服务器会验证客户端的IP地址是否在给定列表中或属于列表中的CIDR范围。If the client's IP address is not present, the server does not authenticate the user.如果客户端的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. 客户端可以连接的IP地址或CIDR范围的列表。如果存在,服务器将验证是否通过给定列表中的IP地址接受了客户端的连接。If the connection was accepted via an unrecognized IP address, the server does not authenticate the user.如果通过无法识别的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.有关MongoDB中身份验证的更多信息,请参阅身份验证

Behavior行为

Replica set副本集

If run on a replica set, db.updateRole() is executed using "majority" write concern by default.如果在副本集上运行,db.updateRole()在默认情况下使用"majority"写入关注执行。

Scope范围

Except for roles created in the admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database.除了在admin数据库中创建的角色外,角色只能包括应用于其数据库的权限,并且只能从其数据库中的其他角色继承。

A role created in the admin database can include privileges that apply to the admin database, other databases or to the cluster resource, and can inherit from roles in other databases as well as the admin database.admin数据库中创建的角色可以包括应用于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.必须对roles数组中每个角色的数据库执行grantRole 操作才能更新数组。

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

  • 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 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:以下db.updateRole()方法将替换products数据库中存在的inventoryControl角色的privilegesroles。该方法在包含inventoryControl的数据库上运行:

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

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