On this page本页内容
updateUser
Updates 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 对字段的更新将完全替换上一个字段的值,包括对用户roles
and authenticationRestrictions
arrays.roles
和authenticationRestrictions
数组的更新。
When you update the 更新roles
array, you completely replace the previous array's values. roles
数组时,将完全替换上一个数组的值。To add or remove roles without replacing all the user's existing roles, use the 要添加或删除角色而不替换所有用户的现有角色,请使用grantRolesToUser
or revokeRolesFromUser
commands.grantRolesToUser
或revokeRolesFromUser
命令。
The updateUser
command uses the following syntax. updateUser
命令使用以下语法。To update a user, you must specify the 要更新用户,必须指定updateUser
field and at least one other field, other than writeConcern
:updateUser
字段和至少一个其他字段(writeConcern
除外):
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将mongo
shell, you can use the passwordPrompt()
method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell那样直接指定密码。
{ 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> }
The command has the following fields:该命令包含以下字段:
updateUser | string | |
pwd | string |
|
customData | document | |
roles | array | roles array overrides the previous array's values.roles 数组的更新将覆盖上一个数组的值。 |
writeConcern | document |
|
authenticationRestrictions | array | |
mechanisms | array |
Valid values are:
|
digestPassword | boolean |
true . In earlier versions, the default value is false .true 。在早期版本中,默认值为false 。
|
comment | any |
|
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.若要指定存在于其他数据库中的角色,请使用文档指定该角色。
The authenticationRestrictions
document can contain only the following fields. authenticationRestrictions
文档只能包含以下字段。The server throws an error if the 如果authenticationRestrictions
document contains an unrecognized field:authenticationRestrictions
文档包含无法识别的字段,服务器将抛出错误:
Field Name | ||
---|---|---|
clientSource | Array of IP addresses and/or CIDR ranges | |
serverAddress |
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.有关MongoDB中身份验证的更多信息,请参阅身份验证。
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.mongod
和mongos
。
MongoDB does not store the password in cleartext. MongoDB不以明文形式存储密码。The password is only vulnerable in transit between the client and the server, and only if TLS transport encryption is not enabled.只有在未启用TLS传输加密的情况下,密码才在客户端和服务器之间传输时易受攻击。
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操作才能向用户添加角色。grantRole
action on a role's database to add a role to a user.
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
操作的权限。
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"), // Starting in MongoDB 4.0.9 "user" : "appClient01", "db" : "products", "customData" : { "empID" : "12345", "badge" : "9156" }, "roles" : [ { "role" : "readWrite", "db" : "products" }, { "role" : "read", "db" : "inventory" } ], "mechanisms" : [ // Starting in MongoDB 4.0 "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"), // Starting in MongoDB 4.0.9 "user" : "appClient01", "db" : "products", "customData" : { "employeeId" : "0x3039" }, "roles" : [ { "role" : "read", "db" : "assets" } ], "mechanisms" : [ // Starting in MongoDB 4.0 "SCRAM-SHA-1", "SCRAM-SHA-256" ] }