Definition定义
createRoleCreates 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.createRole命令将返回重复角色错误。Tip
In在mongosh, this command can also be run through thedb.createRole()helper method.mongosh中,此命令也可以通过db.createRole()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.adminCommand(
{
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>
}
)
Command Fields命令字段
The createRole command has the following fields:createRole命令包含以下字段:
createRole | ||
privileges |
| |
roles |
| |
authenticationRestrictions |
| |
writeConcern | ||
comment |
|
Roles角色
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.若要指定存在于其他数据库中的角色,请使用文档指定该角色。
Authentication Restrictions身份验证限制
The authenticationRestrictions document can contain only the following fields. The server throws an error if the authenticationRestrictions document contains an unrecognized field:authenticationRestrictions文档只能包含以下字段。如果authenticationRestrictions文档包含无法识别的字段,服务器将抛出错误:
| Value | ||
|---|---|---|
clientSource | ||
serverAddress |
Important
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 on Self-Managed Deployments.有关MongoDB中身份验证的更多信息,请参阅自我管理部署的身份验证。
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.admin数据库上创建的角色可以包括应用于所有数据库或集群的权限,并且可以继承其他数据库中角色的权限。
Required Access所需访问权限
To create a role in a database, you must have:要在数据库中创建角色,您必须具备:
the该数据库资源上的createRoleaction on that database resource.createRole操作。the该数据库上的grantRoleaction 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操作。
Example示例
The following 以下createRole command creates the myClusterwideAdmin role on the admin database:createRole命令在管理数据库上创建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 }
})