w3resource

MongoDB: db.changeUserPassword() method

db.changeUserPassword()

The db.changeUserPassword() method is used to changes an existing user’s password.

Syntax:

db.changeUserPassword(username, password)

Updates a user’s password. Run the method in the database where the user is defined, i.e. the database you created the user.

Parameters:

Name Description Required /
Optional
Type
username Specifies an existing username with access privileges for this database. Required string
password Specifies the corresponding password. Required string
mechanism Specifies the authentication mechanism used. Defaults to either:
  • SCRAM-SHA-1 on new 3.0 installations and on 3.0 databases that have been upgraded from 2.6 with authSchemaUpgrade; or
  • MONGODB-CR otherwise.
Optional string
digestPassword Determines whether the server receives digested or undigested password. Set to false to specify undigested password. For use with SASL/LDAP authentication since the server must forward an undigested password to saslauthd. Optional boolean

Example: MongoDB: db.changeUserPassword() method

Given a user mynewuser in the test database with the following user info:

> db.getUser("mynewuser");
{
        "_id" : "test.mynewuser",
        "user" : "mynewuser",
        "db" : "test",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "assets"
                }
        ],
        "customData" : {
                "employeeId" : "0x3039"
        }
}

The following operation changes the password of the user named mynewuser in the test database to user1234.

db.changeUserPassword("mynewuser", "user1234");

Retrieve the restaurants data from here

Required Access

To modify the password of another user on a database, you must have the changeAnyPassword action on that database.

Previous: db.updateUser() method
Next: db.dropAllUsers() method



Follow us on Facebook and Twitter for latest update.