On this page本页内容
db.
grantPrivilegesToRole
(rolename, privileges, writeConcern)¶Grants additional privileges to a user-defined role.向用户定义的角色授予其他权限。
The grantPrivilegesToRole()
method uses the following syntax:grantPrivilegesToRole()
方法使用以下语法:
The grantPrivilegesToRole()
method takes the following arguments:grantPrivilegesToRole()
方法采用以下参数:
rolename |
string | |
privileges |
array | privileges .privileges 。 |
writeConcern |
document | writeConcern document takes the same fields as the getLastError command.writeConcern 文档采用与getLastError 命令相同的字段。 |
The grantPrivilegesToRole()
method can grant one or more privileges. grantPrivilegestRole()
方法可以授予一个或多个权限。Each 每个<privilege>
has the following syntax:<privilege>
语法如下所示:
The db.grantPrivilegesToRole()
method wraps the grantPrivilegesToRole
command.db.grantPrivilegesToRole()
方法包装了grantPrivilegesToRole
命令。
If run on a replica set, 如果在副本集上运行,默认情况下,db.grantPrivilegesToRole()
is executed using majority
write concern by default.db.grantPrivilegesToRole()
将使用majority
写入关注点执行。
Except for roles created in the 除了在管理数据库中创建的角色外,角色只能包含应用于其数据库的权限admin
database, a role can only include privileges that apply to its database
A role created in the 在admin
database can include privileges that apply to the admin
database, other databases or to the cluster resource.admin
数据库中创建的角色可以包括应用于admin
数据库、其他数据库或群集资源的权限。
You must have the 为了授予权限,必须对权限目标数据库执行grantRole
action on the database a privilege targets in order to grant the privilege. grantRole
操作。To grant a privilege on multiple databases or on the 要在多个数据库或群集资源上授予权限,必须对cluster
resource, you must have the grantRole
action on the admin
database.admin
数据库执行grantRole
操作。
The following 以下db.grantPrivilegesToRole()
operation grants two additional privileges to the role inventoryCntrl01
, which exists on the products
database. db.grantPrivilegesToRole()
操作为products
数据库中的角色inventoryCntrl01
授予两个额外的权限。The operation is run on that database:该操作在该数据库上运行:
The first privilege permits users with this role to perform the 第一个权限允许具有此角色的用户对insert
action on all collections of the products
database, except the system collections. products
数据库的所有集合(系统集合除外)执行insert
操作。To access a system collection, a privilege must explicitly specify the system collection in the resource document, as in the second privilege.要访问系统集合,权限必须在资源文档中明确指定系统集合,就像在第二个权限中一样。
The second privilege permits users with this role to perform the 第二个权限允许具有此角色的用户对find
action on the product
database’s system collection named system.js
.product
数据库名为system.js
的系统集合执行find
操作。