grantPrivilegesToRole

在本页面

Definition

{
  grantPrivilegesToRole: "<role>",
  privileges: [
      {
        resource: { <resource> }, actions: [ "<action>", ... ]
      },
      ...
  ],
  writeConcern: { <write concern> }
}

grantPrivilegesToRole命令具有以下字段:

Field Type Description
grantPrivilegesToRole string 授予特权的用户定义角色的名称。
privileges array 添加到角色的特权。有关特权的格式,请参见privileges
writeConcern document 可选的。修改的write concern级别。 writeConcern文档具有与getLastError命令相同的字段。

Behavior

角色的特权适用于创建角色的数据库。在admin数据库上创建的角色可以包括适用于所有数据库或cluster的特权。

Required Access

您必须在数据库上具有grantRole action的特权目标才能授予该特权。要在多个数据库或cluster资源上授予特权,您必须对admin数据库具有grantRole操作。

Example

以下grantPrivilegesToRole命令向products数据库中存在的service角色授予两个附加特权:

use products
db.runCommand(
   {
     grantPrivilegesToRole: "service",
     privileges: [
         {
           resource: { db: "products", collection: "" }, actions: [ "find" ]
         },
         {
           resource: { db: "products", collection: "system.js" }, actions: [ "find" ]
         }
     ],
     writeConcern: { w: "majority" , wtimeout: 5000 }
   }
)

privileges数组中的第一个特权允许用户搜索products数据库中的所有非系统集合。该特权不允许对system collections进行查询,例如system.js集合。要授予对这些系统集合的访问权限,请在privileges数组中显式设置访问权限。参见Resource Document

第二个特权明确允许对所有数据库上的system.js集合执行find操作。

首页