Docs Menu

db.grantPrivilegesToRole()

db.grantPrivilegesToRole(rolename, privileges, writeConcern)

Grants additional privileges to a user-defined role.

重要

mongosh メソッド

このページでは、mongosh メソッドについて記載しています。ただし、データベースコマンドや Node.js などの言語固有のドライバーのドキュメントには該当しません

データベースコマンドについては、grantPrivilegesToRole コマンドを参照してください。

MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。

db.grantPrivilegesToRole()メソッドは次の構文を使用します。

db.grantPrivilegesToRole(
"< rolename >",
[
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
{ < writeConcern > }
)

db.grantPrivilegesToRole()メソッドは次の引数を取ります。

Parameter
タイプ
説明

rolename

string

The name of the role to grant privileges to.

privileges

配列

The privileges to add to the role. For the format of a privilege, see privileges.

writeConcern

ドキュメント

任意。 操作の 書込み保証( write concern ) のレベル。 詳しくは、 書込み保証(write concern) の仕様を参照してください。

The db.grantPrivilegesToRole() method can grant one or more privileges. Each <privilege> has the following syntax:

{ resource: { <resource> }, actions: [ "<action>", ... ] }

このメソッドは、次の環境でホストされている配置で使用できます。

重要

このコマンドは、 MongoDB Atlasクラスターではサポートされていません。 すべてのコマンドの Atlas サポートの詳細については、「 サポートされていないコマンド 」を参照してください。

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

レプリカセットで実行する場合、 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 クラスター resource.

You must have the grantRole アクション on the database a privilege targets in order to grant the privilege. To grant a privilege on multiple databases or on the cluster resource, you must have the grantRole action on the admin database.

The following db.grantPrivilegesToRole() operation grants two additional privileges to the role inventoryCntrl01, which exists on the products database. The operation is run on that database:

use products
db.grantPrivilegesToRole(
"inventoryCntrl01",
[
{
resource: { db: "products", collection: "" },
actions: [ "insert" ]
},
{
resource: { db: "products", collection: "system.js" },
actions: [ "find" ]
}
],
{ w: "majority" }
)

The first privilege permits users with this role to perform the insert アクション on all collections of the products database, except the system collections. 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 アクション on the product database's system collection named system.js.