定義
grantRolesToUserユーザーに追加のロールを付与します。
Tip
In
mongosh, this command can also be run through thedb.grantRolesToUser()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.
互換性
このコマンドは、次の環境でホストされている配置で使用できます。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
重要
このコマンドは、MongoDB Atlas クラスターではサポートされていません。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
構文
The grantRolesToUser command uses the following syntax:
db.runCommand( { grantRolesToUser: "<user>", roles: [ <roles> ], writeConcern: { <write concern> }, comment: <any> } )
コマンドフィールド
このコマンドは、次のフィールドを使用します。
フィールド | タイプ | 説明 |
|---|---|---|
| string | 追加のロールを付与するユーザーの名前。 |
| 配列 | ユーザーに付与する追加のロールの配列。 |
| ドキュメント | 任意。 操作の 書込み保証( write concern ) のレベル。 詳しくは、 書込み保証(write concern) の仕様を参照してください。 |
| any | 任意。このコマンドに添付するユーザー指定のコメント。設定すると、このコメントは以下の場所にこのコマンドの記録と合わせて表示されます。
コメントには、有効な BSON 型(string, integer, object, array など)を使用できます。 |
rolesフィールドでは、組み込みロールとユーザー定義ロールの両方を指定できます。
To specify a role that exists in the same database where grantRolesToUser runs, you can either specify the role with the name of the role:
"readWrite"
または、次のように、ドキュメントを使用してロールを指定することもできます。
{ role: "<role>", db: "<database>" }
別のデータベースに存在するロールを指定するには、 ドキュメントを使用してロールを指定します。
必要なアクセス権
例
products データベースに次のロールを持つユーザー accountUser01 がある場合:
"roles" : [ { "role" : "assetsReader", "db" : "assets" } ]
The following grantRolesToUser operation gives accountUser01 the read role on the stock database and the readWrite role on the products database.
use products db.runCommand( { grantRolesToUser: "accountUser01", roles: [ { role: "read", db: "stock"}, "readWrite" ], writeConcern: { w: "majority" , wtimeout: 2000 } } )
productsデータベース内のユーザー accountUser01 には現在、次のロールがあります。
"roles" : [ { "role" : "assetsReader", "db" : "assets" }, { "role" : "read", "db" : "stock" }, { "role" : "readWrite", "db" : "products" } ]