AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

grantRolesToRole(データベースコマンド)

grantRolesToRole

ユーザー定義ロールにロールを付与します。

Tip

In mongosh, this command can also be run through the db.grantRolesToRole() helper method.

Helper methods are convenient for mongosh users, 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 grantRolesToRole command affects roles on the database where the command runs.

このコマンドの構文は、次のとおりです。

db.runCommand(
{
grantRolesToRole: "<role>",
roles: [
{ role: "<role>", db: "<database>" },
...
],
writeConcern: { <write concern> },
comment: <any>
}
)

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

フィールド
タイプ
説明

grantRolesToRole

string

サブロールを追加するロールの名前。

roles

配列

継承するロールの配列。

writeConcern

ドキュメント

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

comment

any

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

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

rolesフィールドでは、組み込みロールユーザー定義ロールの両方を指定できます。

To specify a role that exists in the same database where grantRolesToRole runs, you can either specify the role with the name of the role:

"readWrite"

または、次のように、ドキュメントを使用してロールを指定することもできます。

{ role: "<role>", db: "<database>" }

別のデータベースに存在するロールを指定するには、 ドキュメントを使用してロールを指定します。

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

  • MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです

重要

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

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

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

ロールはデータベース内の他のロールから権限を継承することができます。admin データベースで作成されたロールは、任意のデータベースのロールから権限を継承できます。

データベースにロールを付与するには、そのデータベースに対してgrantRoleアクションが必要です。

The following grantRolesToRole command updates the productsReaderWriter role in the products database to inherit the privileges of the productsReader role in the products database:

use products
db.runCommand(
{ grantRolesToRole: "productsReaderWriter",
roles: [
"productsReader"
],
writeConcern: { w: "majority" , wtimeout: 5000 }
}
)