MongoDB User Management Methods
The MongoDB user management methods are used to manage the user of the database.
The auth method allows a user within the shell to provide authentication to the database. It can accept either the user name and password, i.e, db.auth( <username>, passwordPrompt() ) Or db.auth( <username>, <password> ).
We can define a user collection that contains the name of the user, password, the mechanism, and a digest password flag.
After connecting the mongo shell, if you want to authenticate, you have to issue db.auth() in the user's authentication database:
#2. 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.
The following operation changes the password of the user named accountUser in the products database to SOh3TbYhx8ypJPxmt1oOfL:
You can also pass the new password directly to db.changeUserPassword():
#3. db.createUser(<user>, <writeConcern>)
This method creates a new user that is specified in the argument for the current database on which the method is currently running. In the case user already exists on the specified database the method return a duplication error.
Syntax to defines the user of the database using createUser method:
The following example will create the accountJTP user on the student database.
#4. db.dropUser(<user>, <writeConcern>)
The db.dropUser() method wraps the dropUser command and removes the user from the current database before dropping a user who has the userAdmin AnyDatabase role. You have to make it clear that you have at least an additional user with user administration privileges.
The following operation drops the jtpAdmin user on the studnet database using db.dropUser().
There is nothing more use of this method. You can use this method to removes the specified username from the current database.
#6. db.updateUser(<username>, <update>, <writeConcern>)
The updateUser method is used to updates the user's profile of the specified database. Using this method will completely replaces the old field's values. This method adds updates to the user's roles array.
The following example will completely replaces the user's customData and roles data using db.updateUser() method: