定义
grantRolesToRole提示
In
mongosh, this command can also be run through thedb.grantRolesToRole()helper method.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.The
grantRolesToRolecommand affects roles on the database where the command runs.
该命令具有以下语法:
db.runCommand( { grantRolesToRole: "<role>", roles: [ { role: "<role>", db: "<database>" }, ... ], writeConcern: { <write concern> }, comment: <any> } )
该命令具有以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 要添加从属角色的角色名称。 |
| 阵列 | 要继承的角色数组。 |
| 文档 | |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
To specify a role that exists in the same database where grantRolesToRole runs, you can either specify the role with the name of the role:
"readWrite"
或者,可以使用文档指定角色,如下所示:
{ role: "<role>", db: "<database>" }
要指定存在于其他数据库中的角色,请使用文档指定该角色。
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
重要
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
一个角色可以从其数据库中的其他角色继承特权。在 admin 数据库上创建的角色可以从任何数据库中的角色继承特权。
必需的访问权限
例子
The following grantRolesToRole command updates the productsReaderWriter role in the products database to inherit the privileges of the productsReader role in the products database:
use products db.runCommand( { grantRolesToRole: "productsReaderWriter", roles: [ "productsReader" ], writeConcern: { w: "majority" , wtimeout: 5000 } } )