Definition定义
db.createRole(role, writeConcern)Creates a role in a database. You can specify privileges for the role by explicitly listing the privileges or by having the role inherit privileges from other roles or both. The role applies to the database on which you run the method.在数据库中创建角色。您可以通过显式列出权限或让角色继承其他角色的权限或两者兼而有之来指定角色的权限。该角色适用于您运行该方法的数据库。Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the关于数据库命令,请参阅createRolecommand.createRole命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Thedb.createRole()method accepts the following arguments:db.createRole()方法接受以下参数:Parameter参数Type类型Description描述roledocument文档A document containing the name of the role and the role definition.包含角色名称和角色定义的文档。writeConcerndocument文档Optional.可选。The level of write concern for the operation.操作的写入关注级别。See Write Concern Specification.请参阅写入关注规范。Theroledocument has the following form:role文档的格式如下:{
role: "<name>",
privileges: [
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>" | "<CIDR range>", ...]
},
...
]
}Theroledocument has the following fields:role文档包含以下字段:Field字段Type类型Description描述rolestring字符串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.privileges数组。You must include the您必须包含privilegesfield. Use an empty array to specify no privileges.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.roles字段。使用空数组不指定要继承的角色。authenticationRestrictionsarray数组Optional.可选。The authentication restrictions the server enforces on the role. Specifies a list of IP addresses and CIDR ranges users granted this role are allowed to connect to and/or which they can connect from.服务器对角色实施的身份验证限制。指定允许授予此角色的用户连接和/或可以从中连接的IP地址和CIDR范围的列表。
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 要指定存在于运行db.createRole() runs, you can either specify the role with the name of the role:db.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文档包含无法识别的字段,服务器将抛出错误:
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中身份验证的更多信息,请参阅自我管理部署的身份验证。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
Important
This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- 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的源代码可用、免费使用和自我管理版本
Behavior行为
Replica set副本集
If run on a replica set, 如果在副本集上运行db.createRole() is executed using "majority" write concern by default.db.createRole(),默认情况下使用"majority"写入关注执行。
Scope范围
Except for roles created in the 除了在admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database.admin数据库中创建的角色外,角色只能包含适用于其数据库的权限,并且只能继承其数据库中的其他角色。
A role created in the 在admin database can include privileges that apply to the admin database, other databases or to the cluster resource, and can inherit from roles in other databases as well as the admin database.admin数据库中创建的角色可以包括应用于admin数据库、其他数据库或集群资源的权限,并且可以继承其他数据库以及管理数据库中的角色。
The 如果角色已存在于数据库中,db.createRole() method returns a duplicate role error if the role already exists in the database.db.createRole()方法将返回重复角色错误。
Privileges权限
When you specify the 指定privileges array, you can specify privileges to apply to multiple collections in a database or to an entire database.privileges数组时,可以指定应用于数据库中的多个集合或整个数据库的权限。
The following syntax specifies privileges on multiple collections in the 以下语法指定了products database.products数据库中多个集合的权限。
privileges: [
{
resource: { db: 'products', collection: 'coll1' },
actions: [ 'bypassDocumentValidation' ]
},
{
resource: { db: 'products', collection: 'coll2' },
actions: [ 'bypassDocumentValidation' ]
}
]
The following syntax specifies privileges on all collections in the 以下语法指定了products database.products数据库中所有集合的权限。
privileges: [
{
resource: { db: 'products', collection: '' },
actions: [ 'bypassDocumentValidation' ]
}
]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.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.
Example示例
The following 以下db.createRole() method creates the myClusterwideAdmin role on the admin database:db.createRole()方法在admin数据库上创建myClusterwideAdmin角色:
use admin
db.createRole(
{
role: "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" }
]
},
{ w: "majority" , wtimeout: 5000 }
)