 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I change a MongoDB user's password?
You need to use changeUserPassword() to change a user’s password. Let us first create a user with some roles. Following is the query to create a user in MongoDB −
> use admin switched to db admin > db.createUser( ...    { ...       user: "Chris", ...       pwd: "chris", ...       roles: [ { role: "readWrite", db: "test" } ] ...    } ... ); Successfully added user: {    "user" : "Chris",    "roles" : [       {          "role" : "readWrite",          "db" : "test"       }    ] } Let us display the user from test database −
> db.getUser("Chris"); This will produce the following output −
{    "_id" : "admin.Chris",    "user" : "Chris",    "db" : "admin",    "roles" : [       {          "role" : "readWrite",          "db" : "test"       }    ],    "mechanisms" : [       "SCRAM-SHA-1",       "SCRAM-SHA-256"    ] } Following is the query to change a MongoDB user's password.
> db.changeUserPassword("Chris", "123456"); The password has been changed.
Advertisements
 