On this page本页内容
db.
updateRole
(rolename, update, writeConcern)¶Updates a user-defined role. 更新用户定义的角色。The db.updateRole()
method must run on the role’s database.db.updateRole()
方法必须在角色的数据库上运行。
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:要在不替换所有值的情况下授予或删除角色或权限,请使用以下一种或多种方法:
db.grantRolesToRole()
db.grantPrivilegesToRole()
db.revokeRolesFromRole()
db.revokePrivilegesFromRole()
Warning
An update to the 对privileges
or roles
array completely replaces the previous array’s values.privileges
或roles
数组的更新将完全替换上一个数组的值。
The updateRole()
method uses the following syntax:updateRole()
方法使用以下语法:
The db.updateRole()
method accepts the following arguments:db.updateRole()
方法接受以下参数:
rolename |
string | |
update |
document | |
writeConcern |
document | writeConcern document takes the same fields as the getLastError command.writeConcern 文档采用与getLastError 命令相同的字段。 |
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. update
文档中的每个字段都是可选的,但文档必须至少包含一个字段。The update
document has the following fields:update
文档包含以下字段:
privileges |
array | roles array. roles 数组,则为必填项。privileges array overrides the previous array’s values. privileges 数组的更新会覆盖上一个数组的值。privileges array.privileges 数组。 |
roles |
array | privileges array. privileges 数组,则为必填项。roles array overrides the previous array’s values.roles 数组的更新会覆盖上一个数组的值。 |
authenticationRestrictions |
array |
|
The db.updateRole()
method wraps the updateRole
command.db.updateRole()
方法包装updateRole
命令。
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()
的同一数据库中存在的角色,可以使用该角色的名称指定该角色:
Or you can specify the role with a document, as in:也可以通过文档指定角色,如中所示:
To specify a role that exists in a different database, specify the role with a document.要指定存在于其他数据库中的角色,请使用文档指定该角色。
New in version 3.6.版本3.6中的新功能。
The authenticationRestrictions
document can contain only the following fields. The server throws an error if the authenticationRestrictions
document contains an unrecognized field:authenticationRestrictions
文档只能包含以下字段。如果authenticationRestrictions
文档包含无法识别的字段,服务器将抛出错误:
clientSource |
||
serverAddress |
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.
For more information on authentication in MongoDB, see Authentication.有关MongoDB中身份验证的更多信息,请参阅身份验证。
If run on a replica set, 如果在副本集上运行,默认情况下,db.updateRole()
is executed using majority
write concern by default.db.updateRole()
将使用majority
写入关注点执行。
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
数据库、其他数据库或群集资源的权限,并且可以从其他数据库以及admin
数据库中的角色继承。
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. grantRole
操作才能更新该数组。If a privilege’s resource spans databases, you must have 如果权限的资源跨越数据库,则必须在grantRole
on the admin
database. admin
数据库上具有grantRole
。A privilege spans databases if the privilege is any of the following:如果权限是以下任一项,则权限跨越数据库:
cluster
resourcecluster
资源You must have the 必须对目标角色的数据库执行setAuthenticationRestriction
action on the database of the target role to update a role’s authenticationRestrictions
document.setAuthenticationRestriction
操作,才能更新角色的authenticationRestrictions
文档。
The following 以下db.updateRole()
method replaces the privileges
and the roles
for the inventoryControl
role that exists in the products
database. db.updateRole()
方法替换产品数据库中存在的inventoryControl
角色的privileges
和roles
。The method runs on the database that contains 该方法在包含inventoryControl
:inventoryControl
的数据库上运行:
To view a role’s privileges, use the 要查看角色的权限,请使用rolesInfo
command.rolesInfo
命令。