![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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