定义
db.updateRole( rolename, update, writeConcern )Updates a user-defined role. The
db.updateRole()method must run on the role's database.重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.有关数据库命令,请参阅
updateRole命令。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
对字段的更新将完全替换先前字段的值。要在不替换所有值的情况下授予或删除角色或权限,请使用以下一种或多种方法:
警告
对
privileges或roles数组的更新将完全替换先前数组的值。The
db.updateRole()method uses the following syntax: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:Parameter类型说明rolename字符串
要更新的用户定义角色的名称。
update文档
一份包含该角色替换数据的文档。该数据完全替代该角色的相应数据。
writeConcern文档
update文档指定了要更新的字段及其新值。update文档中的每个字段都是可选的,但该文档必须至少包含一个字段。update文档包含以下字段:字段类型说明privileges阵列
可选。如果未指定
roles数组,则为必填项。授予角色的权限。privileges数组的更新会覆盖之前数组的值。有关指定权限的语法,请参阅privileges数组。roles阵列
可选。如果未指定
privileges数组,则为必填项。此角色从中继承权限的角色。roles数组的更新会覆盖之前数组的值。authenticationRestrictions阵列
可选。
服务器对角色实施的身份验证限制。指定允许授予此角色的用户连接到和/或可以从中进行连接的 IP 地址和 CIDR 范围列表。
The
db.updateRole()method wraps theupdateRolecommand.
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
角色
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:
"readWrite"
或者,可以使用文档指定角色,如下所示:
{ role: "<role>", db: "<database>" }
要指定存在于其他数据库中的角色,请使用文档指定该角色。
authenticationRestrictions
authenticationRestrictions 文档只能包含以下字段。如果 authenticationRestrictions 文档包含无法识别的字段,服务器会引发错误:
字段名称 | 值 | 说明 |
|---|---|---|
| IP 地址和/或 CIDR 范围的数组 | 如果存在,则在对用户进行身份验证时,服务器会验证客户端的 IP 地址是否在给定列表中或属于列表中的 CIDR 范围。如果客户端的 IP 地址不存在,服务器不会对用户进行身份验证。 |
| IP 地址和/或 CIDR 范围的数组 | 客户端可以连接的 IP 地址或 CIDR 范围列表。如果存在,服务器将验证客户端的连接是否已通过给定列表中的 IP 地址接受。如果通过无法识别的 IP 地址接受连接,服务器不会对用户进行身份验证。 |
重要
如果用户继承的多个角色具有不兼容的身份验证限制,则该用户将不可用。
例如,如果用户继承了一个角色(其中 clientSource 字段为 ["198.51.100.0"])和另一个角色(其中 clientSource 字段为 ["203.0.113.0"]),则服务器无法对该用户进行身份验证。
有关MongoDB中身份身份验证的更多信息,请参阅自托管部署上的身份验证。
行为
副本集
If run on a replica set, db.updateRole() is executed using "majority" write concern by default.
范围
除在 admin 数据库中创建的角色之外,角色只能包含会应用于其数据库的特权,且只能从其数据库中的其他角色来继承。
在 admin 数据库中创建的角色可包含会应用于 admin 数据库、其他数据库或集群资源的特权,且可从其他数据库和 admin 数据库中的角色来继承。
特权
当您指定privileges 大量时,可以指定要应用数据库中的多个集合或整个数据库的权限。
以下语法指定对 products数据库中多个集合的权限。
privileges: [ { resource: { db: 'products', collection: 'coll1' }, actions: [ 'bypassDocumentValidation' ] }, { resource: { db: 'products', collection: 'coll2' }, actions: [ 'bypassDocumentValidation' ] } ]
以下语法指定对 products数据库中所有集合的权限。
privileges: [ { resource: { db: 'products', collection: '' }, actions: [ 'bypassDocumentValidation' ] } ]
必需的访问权限
必须对所有数据库执行 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 命令。