Syntax error How to change the password in MongoDB for existing user?

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: 2019-07-30T22:30:25+05:30

907 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements