How to change the password in MongoDB for existing user?


To change the password in MongoDB for existing user, you can use changeUserPassword(). Following is the syntax

db.changeUserPassword("yourExistingUserName", "yourPassword");

Let us first switch the database to admin. Following is the syntax

> use admin

This will produce the following output

switched to db admin

Now, display users from the database. Following is the query

> db.getUsers();

This will produce the following output

[
   {
      "_id" : "admin.John",
      "user" : "John",
      "db" : "admin",
      "roles" : [
         {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
         }
      ],
      "mechanisms" : [
         "SCRAM-SHA-1",
         "SCRAM-SHA-256"
      ]
   }
]

Following is the query to change the password for user “John”

> db.changeUserPassword("John", "123456");

Now the password has been changed with value “123456”.

Updated on: 30-Jul-2019

629 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements