- Reference >
- Database Commands >
- Role Management Commands >
- createRole
createRole¶
On this page
Definition¶
-
createRole¶ Creates a role and specifies its privileges. The role applies to the database on which you run the command. The
createRolecommand returns a duplicate role error if the role already exists in the database.The
createRolecommand uses the following syntax:The
createRolecommand has the following fields:Field Type Description createRolestring The name of the new role. privilegesarray The privileges to grant the role. A privilege consists of a resource and permitted actions. For the syntax of a privilege, see the
privilegesarray.You must include the
privilegesfield. Use an empty array to specify no privileges.rolesarray An array of roles from which this role inherits privileges.
You must include the
rolesfield. Use an empty array to specify no roles to inherit from.writeConcerndocument Optional. The level of write concern to apply to this operation. The writeConcerndocument uses the same fields as thegetLastErrorcommand.In the
rolesfield, you can specify both built-in roles and user-defined role.To specify a role that exists in the same database where
createRoleruns, you can either specify the role with the name of the role:Or you can specify the role with a document, as in:
To specify a role that exists in a different database, specify the role with a document.
Behavior¶
A role’s privileges apply to the database where the role is created. The
role can inherit privileges from other roles in its database. A role
created on the admin database can include privileges that apply to all
databases or to the cluster and can inherit
privileges from roles in other databases.
Required Access¶
To create a role in a database, you must have:
- the
createRoleaction on that database resource. - the
grantRoleaction on that database to specify privileges for the new role as well as to specify roles to inherit from.
Built-in roles userAdmin and
userAdminAnyDatabase provide createRole and
grantRole actions on their respective resources.
Example¶
The following createRole command creates the
myClusterwideAdmin role on the admin database: