Definition定义
updateUserUpdates the user's profile on the database on which you run the command. An update to a field completely replaces the previous field's values, including updates to the user's更新运行命令的数据库上的用户配置文件。对字段的更新将完全替换前一个字段的值,包括对用户rolesandauthenticationRestrictionsarrays.roles和authenticationRestrictions数组的更新。Tip
In在mongosh, this command can also be run through thedb.changeUserPassword()helper method.mongosh中,此命令也可以通过db.changeUserPassword()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, but they may not return the same level of information as database commands.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。Warning
When you update the更新roles数组时,将完全替换前一个数组的值。rolesarray, you completely replace the previous array's values.To add or remove roles without replacing all the user's existing roles, use the要添加或删除角色而不替换用户的所有现有角色,请使用grantRolesToUserorrevokeRolesFromUsercommands.grantRolesToUser或revokeRolesFromUser命令。To update a user, you must specify the要更新用户,您必须指定updateUser字段和至少一个其他字段,而不是updateUserfield and at least one other field, other thanwriteConcern.writeConcern。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
Syntax语法
The command uses the following syntax:该命令使用以下语法:
db.runCommand(
{
updateUser: "<username>",
pwd: passwordPrompt(), // Or "<cleartext password>"
customData: { <any information> },
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>", | "<CIDR range>", ...]
},
...
],
mechanisms: [ "<scram-mechanism>", ... ],
digestPassword: <boolean>,
writeConcern: { <write concern> },
comment: <any>
}
)Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
updateUser | ||
pwd |
| |
customData | ||
roles | roles array overrides the previous array's values.roles数组的更新会覆盖前一个数组的值。 | |
writeConcern | ||
authenticationRestrictions | ||
mechanisms |
| |
digestPassword |
| |
comment |
|
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 要指定存在于运行updateUser runs, you can either specify the role with the name of the role:updateUser的同一数据库中的角色,您可以使用角色的名称指定角色:
"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文档包含无法识别的字段,服务器将抛出错误:
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.clientSource字段为["198.51.100.0"],另一个角色的clientSource字段是["203.0.113.0"],则服务器无法对用户进行身份验证。
For more information on authentication in MongoDB, see Authentication on Self-Managed Deployments.有关MongoDB中身份验证的更多信息,请参阅自我管理部署的身份验证。
Behavior行为
Warning
By default, 默认情况下,updateUser sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt(). updateUser以明文形式将所有指定的数据发送到MongoDB实例,即使使用passwordPrompt()。Use TLS transport encryption to protect communications between clients and the server, including the password sent by 使用TLS传输加密来保护客户端和服务器之间的通信,包括updateUser. updateUser发送的密码。For instructions on enabling TLS transport encryption, see Configure 有关启用TLS传输加密的说明,请参阅在自我管理部署上为TLS/SSL配置mongod and mongos for TLS/SSL on Self-Managed Deployments.mongod和mongos。
MongoDB does not store the password in cleartext. The password is only vulnerable in transit between the client and the server, and only if TLS transport encryption is not enabled.MongoDB不以明文形式存储密码。密码仅在客户端和服务器之间传输时易受攻击,并且只有在未启用TLS传输加密的情况下才易受攻击。
Required Access所需访问权限
You must have access that includes the 为了更新用户的revokeRole action on all databases in order to update a user's roles array.roles数组,您必须对所有数据库具有包括revokeRole操作的访问权限。
You must have the 您必须对角色的数据库执行grantRole action on a role's database to add a role to a user.grantRole操作,才能向用户添加角色。
To change another user's 要更改其他用户的pwd or customData field, you must have the changePassword and changeCustomData actions respectively on that user's database.pwd或customData字段,您必须在该用户的数据库上分别执行changePassword和changeCustomData操作。
To modify your own password and custom data, you must have privileges that grant 要修改自己的密码和自定义数据,您必须具有在用户数据库上分别授予changeOwnPassword and changeOwnCustomData actions respectively on the user's database.changeOwnPassword和changeOwnCustomData操作的权限。
Example示例
Given a user 给定appClient01 in the products database with the following user info: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"
]
}
The following 以下updateUser command completely replaces the user's customData and roles data:updateUser命令完全替换了用户的customData和roles数据:
use products
db.runCommand( {
updateUser : "appClient01",
customData : { employeeId : "0x3039" },
roles : [ { role : "read", db : "assets" } ]
} )
The user appClient01 in the products database now has the following user information: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"
]
}