OPTIONS
翻译或纠错本页面

createRole

Definition

createRole

Creates a role and specifies its privileges. The role applies to the database on which you run the command. The createRole command returns a duplicate role error if the role already exists in the database.

The createRole command uses the following syntax:

{ createRole: "<new role>",
  privileges: [
    { resource: { <resource> }, actions: [ "<action>", ... ] },
    ...
  ],
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ],
  writeConcern: <write concern document>
}

The createRole command has the following fields:

Field Type Description
createRole string The name of the new role.
privileges array The privileges to grant the role. A privilege consists of a resource and permitted actions. You must specify the privileges field. Use an empty array to specify no privileges. For the syntax of a privilege, see the privileges array.
roles array An array of roles from which this role inherits privileges. You must specify the roles field. Use an empty array to specify no roles.
writeConcern document Optional. The level of write concern to apply to this operation. The writeConcern document uses the same fields as the getLastError command.

In the roles field, you can specify both built-in roles and user-defined role.

To specify a role that exists in the same database where createRole runs, you can either specify the role with the name of the role:

"readWrite"

Or you can specify the role with a document, as in:

{ role: "<role>", db: "<database>" }

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

You must have the createRole action on a database to create a role on that database.

You must have the grantRole action on the database that a privilege targets in order to grant that privilege to a role. If the privilege targets multiple databases or the cluster resource , you must have the grantRole action on the admin database.

You must have the grantRole action on a role’s database to grant the role to another role.

Example

The following createRole command creates the myClusterwideAdmin role on the admin database:

use admin
db.runCommand({ createRole: "myClusterwideAdmin",
  privileges: [
    { resource: { cluster: true }, actions: [ "addShard" ] },
    { resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
    { resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
    { resource: { db: "", collection: "" }, actions: [ "find" ] }
  ],
  roles: [
    { role: "read", db: "admin" }
  ],
  writeConcern: { w: "majority" , wtimeout: 5000 }
})