Navigation
This version of the documentation is archived and no longer supported.

db.updateUser()

Definition

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:

db.updateUser(
   "<username>",
   {
     customData : { <any information> },
     roles : [
               { role: "<role>", db: "<database>" } | "<role>",
               ...
             ],
     pwd: "<cleartext password>"
    },
    writeConcern: { <write concern> }
)

The db.updateUser() method has the following arguments.

Parameter Type Description
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 Optional. The level of write concern for the update operation. The 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:

Field Type Description
customData document Optional. Any arbitrary information.
roles array Optional. The roles granted to the user. An update to the roles array overrides the previous array’s values.
pwd string Optional. The user’s password.

In the roles field, you can specify both built-in roles and user-defined role.

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"

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 db.updateUser() method wraps the updateUser command.

Behavior

db.updateUser() sends password to the MongoDB instance without encryption. To encrypt the password during transmission, use TLS/SSL.

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 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.

Example

Given a user appClient01 in the products database with the following user info:

{
   "_id" : "products.appClient01",
   "user" : "appClient01",
   "db" : "products",
   "customData" : { "empID" : "12345", "badge" : "9156" },
   "roles" : [
       { "role" : "readWrite",
         "db" : "products"
       },
       { "role" : "read",
         "db" : "inventory"
       }
   ]
}

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"  }
                         ]
                }
             )

The user appClient01 in the products database now has the following user information:

{
   "_id" : "products.appClient01",
   "user" : "appClient01",
   "db" : "products",
   "customData" : { "employeeId" : "0x3039" },
   "roles" : [
       { "role" : "read",
         "db" : "assets"
       }
   ]
}