Docs Menu

grantPrivilegesToRole

grantPrivilegesToRole

Assigns additional privileges to a user-defined role defined on the database on which the command is run.

Tip

mongoshでは、このコマンドはdb.grantPrivilegesToRole()ヘルパー メソッドを通じて実行することもできます。

ヘルパー メソッドはmongoshユーザーには便利ですが、データベースコマンドと同じレベルの情報は返されない可能性があります。 便宜上必要ない場合、または追加の戻りフィールドが必要な場合は、 データベースコマンドを使用します。

grantPrivilegesToRoleコマンドは、次の構文を使用します。

db.runCommand(
{
grantPrivilegesToRole: "<role>",
privileges: [
{
resource: { <resource> }, actions: [ "<action>", ... ]
},
...
],
writeConcern: { <write concern> },
comment: <any>
}
)

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

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

重要

このコマンドは、M0、M2、M5、および Flex クラスターではサポートされていません。詳細については、「 サポートされていないコマンド 」を参照してください。

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

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

コマンドには次のフィールドがあります:

フィールド
タイプ
説明

grantPrivilegesToRole

string

The name of the user-defined role to grant privileges to.

privileges

配列

ロールに追加する特権。 権限の形式については、 privilegesを参照してください。

writeConcern

ドキュメント

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

comment

any

任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。

コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。

A role's privileges apply to the database where the role is created. A role created on the admin database can include privileges that apply to all databases or to the クラスター.

特権を付与するには、特権ターゲットのデータベースに対してgrantRoleアクションが必要です。 複数のデータベースまたはclusterリソースに特権を付与するには、 adminデータベースに対してgrantRoleアクションが必要です。

The following grantPrivilegesToRole command grants two additional privileges to the service role that exists in the products database:

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 }
}
)

The first privilege in the privileges array allows the user to search on all non-system collections in the products database. The privilege does not allow queries on system collections, such as the system.js collection. To grant access to these system collections, explicitly provision access in the privileges array. See 自己管理型配置に関するリソース ドキュメント.

The second privilege explicitly allows the find action on system.js collections on all databases.