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
orroles
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 theupdate
document is optional, but the document must include at least one field. Theupdate
document has the following fields:字段类型说明privileges
阵列
可选。如果未指定
roles
数组,则为必填项。授予角色的权限。privileges
数组的更新会覆盖之前数组的值。有关指定权限的语法,请参阅privileges
数组。roles
阵列
可选。如果未指定
privileges
数组,则为必填项。此角色从中继承权限的角色。roles
数组的更新会覆盖之前数组的值。authenticationRestrictions
阵列
可选。
服务器对角色实施的身份验证限制。指定允许授予此角色的用户连接到和/或可以从中进行连接的 IP 地址和 CIDR 范围列表。
版本 3.6 中的新增功能。
db.updateRole()
方法封装了updateRole
命令。
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas集群不支持此命令。 有关Atlas支持所有命令的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
角色
要指定运行 db.updateRole()
的同一数据库中存在的角色,可以使用角色名称指定角色:
"readWrite"
或者,可以使用文档指定角色,如下所示:
{ role: "<role>", db: "<database>" }
要指定存在于其他数据库中的角色,请使用文档指定该角色。
authenticationRestrictions
版本 3.6 中的新增功能。
authenticationRestrictions
文档只能包含以下字段。如果 authenticationRestrictions
文档包含无法识别的字段,服务器会引发错误:
字段名称 | 值 | 说明 |
---|---|---|
| IP 地址和/或 CIDR 范围的数组 | 如果存在,则在对用户进行身份验证时,服务器会验证客户端的 IP 地址是否在给定列表中或属于列表中的 CIDR 范围。如果客户端的 IP 地址不存在,服务器不会对用户进行身份验证。 |
| 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
命令。