On this page本页内容
db.updateUser(username, update, writeConcern)¶Updates the user’s profile on the database on which you run the method. An update to a field completely replaces the previous field’s values. This includes updates to the user’s roles array.
Warning
When you update the roles array, you completely replace the previous array’s values. To add or remove roles without replacing all the user’s existing roles, use the db.grantRolesToUser() or db.revokeRolesFromUser() methods.
The db.updateUser() method uses the following syntax:
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. However, you can still specify the password directly as you would with earlier versions of the mongo shell.
The db.updateUser() method has the following arguments.
username |
string | The name of the user to update. |
update |
document | A document containing the replacement data for the user. This data completely replaces the corresponding data for the user. |
writeConcern |
document | writeConcern document takes the same fields as the getLastError command. |
The update document specifies the fields to update and their new values. All fields in the update document are optional, but must include at least one field.
The update document has the following fields:
customData |
document | |
roles |
array | roles array overrides the previous array’s values. |
pwd |
string |
Tip Starting in version 4.2 of the |
authenticationRestrictions |
array |
|
mechanisms |
array |
If updating the mechanisms field without the password, you can only specify a subset of the user’s current mechanisms, and only the existing user credentials for the specified mechanism or mechanisms are retained. If updating the password along with the mechanisms, new set of credentials are stored for the user. Valid values are:
|
passwordDigestor |
string |
Available values are:
|
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 db.updateUser() runs, you can either specify the role with the name of the role:
Or you can specify the role with a document, as in:
To specify a role that exists in a different database, specify the role with a document.
New in version 3.6.版本3.6中的新功能。
The authenticationRestrictions document can contain only the following fields. The server throws an error if the authenticationRestrictions document contains an unrecognized field:
| Field Name | Value | |
|---|---|---|
clientSource |
Array of IP addresses and/or CIDR ranges | If present, when authenticating a user, the server verifies that the client’s IP address is either in the given list or belongs to a CIDR range in the list. If the client’s IP address is not present, the server does not authenticate the user. |
serverAddress |
Array of IP addresses and/or CIDR ranges | A list of IP addresses or CIDR ranges to which the client can connect. If present, the server will verify that the client’s connection was accepted via an IP address in the given list. If the connection was accepted via an unrecognized IP address, the server does not authenticate the user. |
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.
For more information on authentication in MongoDB, see Authentication.
The db.updateUser() method wraps the updateUser command.
If run on a replica set, db.updateUser() is executed using majority write concern by default.
Warning
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 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.
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 changeAnyPassword and changeAnyCustomData 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.
Given a user appClient01 in the products database with the following user info:
The following db.updateUser() method completely replaces the user’s customData and roles data:
The user appClient01 in the products database now has the following user information:
SCRAM-SHA-256 Credentials Only¶Note
To use SCRAM-SHA-256, the featureCompatibilityVersion must be set to 4.0. For more information on featureCompatibilityVersion, see View FeatureCompatibilityVersion and setFeatureCompatibilityVersion.
The following operation updates a user who currently have both SCRAM-SHA-256 and SCRAM-SHA-1 credentials to have only SCRAM-SHA-256 credentials.
Note
mechanisms, you can only update the mechanisms to a subset of the current SCRAM mechanisms for the user.mechanisms, you can specify any supported SCRAM mechanism or mechanisms.SCRAM-SHA-256, the passwordDigestor must be the default value "server".