定义
db.updateUser( username, update, writeConcern )更新运行该方法的数据库上的用户配置文件。字段更新将完全替换先前字段的值。这包括对用户的
roles数组的更新。警告
roles数组的更新会完全替换之前数组的值。要添加或删除角色而不替换用户的所有现有角色,请使用db.grantRolesToUser()或db.revokeRolesFromUser()方法。The
db.updateUser()method uses the following syntax:提示
您可以将
passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的mongoshell 一样直接指定密码。db.updateUser( "<username>", { customData : { <any information> }, roles : [ { role: "<role>", db: "<database>" } | "<role>", ... ], pwd: passwordPrompt(), // Or "<cleartext password>" authenticationRestrictions: [ { clientSource: ["<IP>" | "<CIDR range>", ...], serverAddress: ["<IP>", | "<CIDR range>", ...] }, ... ], mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ], passwordDigestor: "<server|client>" }, writeConcern: { <write concern> } ) The
db.updateUser()method has the following arguments.Parameter类型说明username字符串
要更新的用户的名称。
update文档
一份包含该用户替换数据的文档。该数据完全替代该用户的相应数据。
writeConcern文档
update文档指定了要更新的字段及其新值。update文档中的所有字段都是可选的,但必须 至少包含一个字段。update文档包含以下字段:字段类型说明customData文档
可选。任意信息。
roles阵列
可选。 授予用户的角色。
roles数组的更新会覆盖之前数组的值。pwd字符串
可选。用户的密码。该值可为:
以明文字符串表示的用户密码,或
passwordPrompt()提示输入用户密码。
您可以将
passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的mongoshell 一样直接指定密码。authenticationRestrictions阵列
可选。服务器对用户实施的身份验证限制。指定 IP 地址和 CIDR 范围列表,允许这些地址和范围的用户连接至服务器或者服务器接受这些用户。
mechanisms阵列
可选。针对用户档案的特定 SCRAM 机制。如果指定了
authenticationMechanisms,则只能指定部分authenticationMechanisms。如果在没有密码的情况下更新机制字段,则只能指定用户当前机制的子集,并且仅保留指定机制或机制的现有用户档案。
如果同时更新密码和机制,则会为用户存储一组新的档案。
有效值为:
"SCRAM-SHA-1"- 使用
SHA-1哈希函数。
"SCRAM-SHA-256"使用
SHA-256哈希函数。需将 featureCompatibilityVersion 设为
4.0。要求 passwordDigestor 为
server。
passwordDigestor字符串
可选。指示对密码进行摘要处理的是服务器还是客户端。
可用值为:
"server"(默认)- 服务器会从客户端接收未加密的密码,然后对此密码进行加密。
"client"(与SCRAM-SHA-256不兼容)- 客户端会对该密码进行哈希,并将哈希后的密码传递给服务器。
角色
To specify a role that exists in the same database where db.updateUser() 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中身份身份验证的更多信息,请参阅自托管部署上的身份验证。
The db.updateUser() method wraps the updateUser command.
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
副本集
If run on a replica set, db.updateUser() is executed using "majority" write concern by default.
加密
警告
By default, db.updateUser() sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). Use TLS transport encryption to protect communications between clients and the server, including the password sent by db.updateUser(). For instructions on enabling TLS transport encryption, see Configure MongoDB Instances for TLS/SSL Encryption.
MongoDB 不以明文形式存储密码。仅当未启用 TLS 传输加密时,密码才会在客户端与服务器之间的传输过程中容易受到攻击。
必需的访问权限
要更新用户的 roles 数组,您必须具有包含所有数据库上的revokeRole动作的访问权限。
必须对某一角色的数据库执行 grantRole 操作才能向用户添加角色。
要更改其他用户的 pwd 或 customData 字段,必须对该用户的数据库分别执行 changePassword 和 changeCustomData 操作。
要修改自己的密码和自定义数据,您必须具有分别授予在用户数据库上执行 changeOwnPassword 和 changeOwnCustomData 动作的特权。
例子
假定在 products 数据库中有用户 appClient01,其用户信息如下:
{ _id : "products.appClient01", userId : UUID("c5d88855-3f1e-46cb-9c8b-269bef957986"), user : "appClient01", db : "products", customData : { empID : "12345", badge : "9156" }, roles : [ { role : "readWrite", db : "products" }, { role : "read", db : "inventory" } ], mechanisms : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ], authenticationRestrictions : [ { clientSource: ["69.89.31.226"], serverAddress: ["172.16.254.1"] } ] }
The following db.updateUser() method completely replaces the user's customData and roles data:
use products db.updateUser( "appClient01", { customData : { employeeId : "0x3039" }, roles : [ { role : "read", db : "assets" } ] } )
products 数据库中的用户 appClient01 现在具有以下用户信息:
{ _id : "products.appClient01", userId : UUID("c5d88855-3f1e-46cb-9c8b-269bef957986"), user : "appClient01", db : "products", customData : { employeeId : "0x3039" }, roles : [ { role : "read", db : "assets" } ], mechanisms : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ], authenticationRestrictions : [ { clientSource: ["69.89.31.226"], serverAddress: ["172.16.254.1"] } ] }
更新用户以仅使用 SCRAM-SHA-256 凭证
注意
要使用 SCRAM-SHA-256,必须将 featureCompatibilityVersion 设置为 4.0。有关 featureCompatibilityVersion 的更多信息,请参阅获取 FeatureCompatibilityVersion 和 setFeatureCompatibilityVersion。
以下操作会将当前同时拥有 SCRAM-SHA-256 和 SCRAM-SHA-1 档案的用户更新为仅拥有 SCRAM-SHA-256 档案。
注意
如果密码没有与
mechanisms一起指定,则只能将mechanisms更新为用户当前 SCRAM 机制的子集。如果密码与
mechanisms一起指定,您可以指定任何受支持的 SCRAM 机制。对于
SCRAM-SHA-256,passwordDigestor必须是默认值"server"。
use reporting db.updateUser( "reportUser256", { mechanisms: [ "SCRAM-SHA-256" ] } )