MongoDB Role Management Commands
The role management commands are used to define the role for the specified user.
MongoDB createRole command
The createRole command assigns a role and specifies its advantages. The assigned role applies to the database on which we run the command. The command returns a duplicate role error if the role already exists in the database.
Syntax:
Command fields:
Field | Type | Description |
---|---|---|
createRole | string | The createRole field contains the name of the new role. |
privileges | array | It contains the privileges to grant the roles. Left it blanks if you don’t want to specify any role. |
roles | array | It contains the array of roles which is used to assign the role to the user. |
authentication Restrictions | array | The authentication restriction field restricts the server from enforcing on the role. |
writeConcern | document | It is the level of write concern to apply to this operation. |
Example:
The createRole command creates the tutoraspireAdmin role on the admin database
MongoDB dropRole command
The MongoDB dropRole command deletes the role that is defined by the user in the database on which we run the command.
Syntax:
MongoDB updateRole
The update command updates the user-defined role. The command must run on the role’s database. This command can replace the previous field value completely.
Syntax:
Example:
The above example updates the myClusterwideAdmin role on the admin database.
MongoDB grantPrivilagesToRole command
This is a very important command which is used to add some extra privileges to a user-defined role on the database where the command is used to run.
Syntax:
Example:
The above example grants two additional privilages to the service role that exists in the product database.