How to drop a User?
In Mongodb, we can also drop a user using dropUser() method. This method returns true when the user is deleted otherwise return false.
Syntax:
db.dropUser(“Username”)
Example:
In this example, we will drop a user whose name is Robert.
db.dropUser("robert")
Create user and add role in MongoDB
In MongoDB, we are allowed to create new users for the database. Every MongoDB user only accesses the data that is required for their role. A role in MongoDB grants privileges to perform some set of operations on a given resource. In MongoDB, users are created using createUser() method. This method creates a new user for the database, if the specified user is already present in the database then this method will return an error.
Syntax:
db.createUser(user, writeConcern)
Parameters:
1. user: It contains authentication and access information about the user to create. It is a document.
- user: Name of the user
- pwd: User password. This field is not required if you use this method on $external database to create a user whose credentials are stored externally. The value of this field can be of string type or passwordPrompt().
- customData: User Associative Information. It is an optional field.
- roles: Access Level or Privilege of a user. You can also create a user without roles by passing an empty array[]. In this field, you use built-in roles or you can create you own role using db.createRole(role, writeConcern) method. To specify the roles you can use any of the following syntax:
Simply specify the role name:
“read”
Or you can specify a document that contains the role and db fields. It is generally used when the role is specified in a different database.
{role:<role>, db: <database>}
- authenticationRestrictions: Authentication permission of the user. It is an optional field.
- mechanisms: It is used to specify the SCRM mechanisms or mechanisms for creating SCRM user credentials. It is an optional field.
- passwordDigestor: It is used to check whether the server or client digest the password. It is an optional field.
2. writeConcern: It is an optional parameter. It manages the level of Write Concern for the creation operation. It takes the same field as the getLastError Command takes.
Notes:
- In MongoDB, the first created user in the database must be the admin user. The admin user has the privileges to maintain all the users. Also, you are not allowed to create users in the local database.
- db.createUser() Sends Password And All Other Data to The MongoDB Instance Without Any Encryption. To Encrypt the Password During Transmission, Use TLS/SSL In order To Encrypt It.
Contact Us