On this page本页内容
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.createRole
命令将返回重复的角色错误。
The createRole
command uses the following syntax:createRole
命令使用以下语法:
{ createRole: "<new role>", privileges: [ { resource: { <resource> }, actions: [ "<action>", ... ] }, ... ], roles: [ { role: "<role>", db: "<database>" } | "<role>", ... ], authenticationRestrictions: [ { clientSource: ["<IP>" | "<CIDR range>", ...], serverAddress: ["<IP>" | "<CIDR range>", ...] }, ... ], writeConcern: <write concern document>, comment: <any> }
The createRole
command has the following fields:createRole
命令包含以下字段:
createRole | string | |
privileges | array |
|
roles | array |
|
authenticationRestrictions | array | |
writeConcern | document |
|
comment | any |
|
In the 在roles
field, you can specify both built-in roles and user-defined roles.roles
字段中,可以指定内置角色和用户定义的角色。
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:createRole
的同一数据库中的角色,可以使用角色名称指定角色:
"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.要指定存在于其他数据库中的角色,请使用文档指定该角色。
The authenticationRestrictions
document can contain only the following fields. authenticationRestrictions
文档只能包含以下字段。The server throws an error if the 如果authenticationRestrictions
document contains an unrecognized field:authenticationRestrictions
文档包含无法识别的字段,则服务器会引发错误:
clientSource | ||
serverAddress |
If a user inherits multiple roles with incompatible authentication restrictions, that user becomes unusable.如果用户继承了具有不兼容身份验证限制的多个角色,则该用户将不可用。
For example, if a user inherits one role in which the 例如,如果一个用户继承了clientSource
field is ["198.51.100.0"]
and another role in which the clientSource
field is ["203.0.113.0"]
the server is unable to authenticate the user.clientSource
字段为["198.51.100.0"]
的一个角色和clientSource
字段为["203.0.113.0"]
的另一个角色,则服务器无法对该用户进行身份验证。
For more information on authentication in MongoDB, see Authentication.有关MongoDB中身份验证的更多信息,请参阅身份验证。
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.admin
数据库上创建的角色可以包括应用于所有数据库或集群的权限,并且可以从其他数据库中的角色继承权限。
To create a role in a database, you must have:要在数据库中创建角色,您必须具备:
createRole
action on that database resource.createRole
操作。grantRole
action on that database to specify privileges for the new role as well as to specify roles to inherit from.grantRole
操作,以指定新角色的权限以及指定要从中继承的角色。Built-in roles 内置角色userAdmin
and userAdminAnyDatabase
provide createRole
and grantRole
actions on their respective resources.userAdmin
和userAdminAnyDatabase
在各自的资源上提供createRole
和grantRole
操作。
To create a role with 要创建指定了authenticationRestrictions
specified, you must have the setAuthenticationRestriction
action on the database resource which the role is created.authenticationRestrictions
的角色,必须对创建角色的数据库资源执行setAuthenticationRestriction
操作。
The following 以下createRole
command creates the myClusterwideAdmin
role on the admin
database:createRole
命令在admin
数据库上创建myClusterwideAdmin
角色:
db.adminCommand({ 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 } })