定義
db.grantPrivilegesToRole(rolename, privileges, writeConcern)ユーザー定義の ロールに追加の 権限 を付与します。
重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.データベースコマンドについては、
grantPrivilegesToRoleコマンドを参照してください。MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
The
db.grantPrivilegesToRole()method uses the following syntax:db.grantPrivilegesToRole( "< rolename >", [ { resource: { <resource> }, actions: [ "<action>", ... ] }, ... ], { < writeConcern > } ) The
db.grantPrivilegesToRole()method takes the following arguments:Parameterタイプ説明rolenamestring
特権を付与するロールの名前。
privileges配列
ロールに追加する特権。 権限の形式については、
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 のバージョン
動作
レプリカセット
If run on a replica set, db.grantPrivilegesToRole() is executed using "majority" write concern by default.
スコープ
adminデータベースで作成されたロールを除き、ロールにはそのデータベースに適用される権限のみを含めることができます
adminデータベースで作成されたロールには、 adminデータベース、他のデータベース、またはクラスターリソースに適用する特権を含めることができます。
権限
privileges 配列を指定すると、データベース内の複数のコレクションまたはデータベース全体に適用する特権を指定できます。
次の構文は、productsデータベース内の複数のコレクションに対する特権を指定します。
privileges: [ { resource: { db: 'products', collection: 'coll1' }, actions: [ 'bypassDocumentValidation' ] }, { resource: { db: 'products', collection: 'coll2' }, actions: [ 'bypassDocumentValidation' ] } ]
次の構文は、productsデータベース内のすべてのコレクションに対する特権を指定します。
privileges: [ { resource: { db: 'products', collection: '' }, actions: [ 'bypassDocumentValidation' ] } ]
必要なアクセス権
特権を付与するには、特権ターゲットのデータベースに対してgrantRoleアクションが必要です。 複数のデータベースまたはclusterリソースに特権を付与するには、 adminデータベースに対してgrantRoleアクションが必要です。
例
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" } )
最初の特権は、このロールを持つユーザーが、 システム コレクション を除く、 insertデータベースのすべてのコレクションに対して アクションproducts を実行することを許可します。システム コレクションにアクセスするには、特権が 2 番目の特権のように、リソース ドキュメントでシステム コレクションを明示的に指定する必要があります。
2 番目の特権は、このロールを持つユーザーがfind productという名前の データベースのシステム コレクションに対して アクションsystem.js を実行することを許可します。