定義
grantPrivilegesToRoleコマンドが実行されるデータベースで定義された ユーザー定義 のロールに追加の 権限 を割り当てます。
Tip
In
mongosh, this command can also be run through thedb.grantPrivilegesToRole()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
grantPrivilegesToRolecommand uses the following syntax:
db.runCommand( { grantPrivilegesToRole: "<role>", privileges: [ { resource: { <resource> }, actions: [ "<action>", ... ] }, ... ], writeConcern: { <write concern> }, comment: <any> } )
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
重要
このコマンドは、M0 および Flex クラスターではサポートされていません。詳細については、サポートされていないコマンド を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
コマンドフィールド
コマンドには次のフィールドがあります:
フィールド | タイプ | 説明 |
|---|---|---|
| string | 特権を付与するユーザー定義ロールの名前。 |
| 配列 | ロールに追加する特権。 権限の形式については、 |
| ドキュメント | 任意。 操作の 書込み保証( write concern ) のレベル。 詳しくは、 書込み保証(write concern) の仕様を参照してください。 |
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。 |
動作
ロールの特権は、ロールが作成されたデータベースに適用されます。 adminデータベースで作成されたロールには、すべてのデータベースまたはクラスターに適用する特権を含めることができます。
必要なアクセス権
特権を付与するには、特権ターゲットのデータベースに対して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 } } )
privileges配列の最初の特権により、ユーザーはproductsデータベース内のすべての非システム コレクションを検索できます。 特権では、 コレクションなどのsystem.js システム コレクション に対するクエリは許可されません。これらのシステム コレクションへのアクセスを許可するには、 privileges配列でアクセスを明示的にプロビジョニングします。 自己管理型配置に関するリソース ドキュメントを参照してください。
2find system.js番目の特権は、すべてのデータベース上の コレクションに対する アクションを明示的に許可します。