updateUser
On this page本页内容
Definition定义
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对字段的更新将完全替换上一个字段的值,包括对用户rolesandauthenticationRestrictionsarrays.roles和authenticationRestrictions数组的更新。TipIn在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.如果不需要方便,或者需要额外的返回字段,请使用数据库命令。WarningWhen you update the更新rolesarray, you completely replace the previous array's values.roles数组时,将完全替换上一个数组的值。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若要更新用户,必须指定updateUserfield and at least one other field, other thanwriteConcern.updateUser字段和除writeConcern之外的至少一个其他字段。
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 | string | |
pwd | string |
Tip Starting in version 4.2 of the 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. mongo shell. |
customData | document | |
roles | array | roles array overrides the previous array's values.roles数组的更新将覆盖上一个数组的值。 |
writeConcern | document | |
authenticationRestrictions | array | |
mechanisms | array | authenticationMechanisms is specified, you can only specify a subset of the authenticationMechanisms.
|
digestPassword | boolean | true (default), the server receives undigested password from the client and digests the password.true(默认值),服务器将从客户端接收未消化的密码并对密码进行消化。false, the client digests the password and passes the digested password to the server. false,客户端将对密码进行摘要处理,并将摘要处理后的密码传递给服务器。SCRAM-SHA-256 SCRAM-SHA-256不兼容 |
comment | any |
|
Roles角色
In the roles field, you can specify both built-in roles and user-defined 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:
"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 | Array of IP addresses and/or CIDR ranges | |
serverAddress | Array of IP addresses and/or CIDR ranges |
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中身份验证的更多信息,请参阅身份验证。
Behavior行为
By default, 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 updateUser. For instructions on enabling TLS transport encryption, see Configure mongod and mongos for TLS/SSL.
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.
You must have the 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.
To modify your own password and custom data, you must have privileges that grant changeOwnPassword and changeOwnCustomData actions respectively on the user's database.
Example实例
Given a user appClient01 in the products database with the following user info:
{
"_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:
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:
{
"_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"
]
}