定義
db.grantRolesToUser(username, roles, writeConcern)ユーザーに追加のロールを付与します。
重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.データベースコマンドについては、
grantRolesToUserコマンドを参照してください。MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
The
db.grantRolesToUser()method uses the following syntax:db.grantRolesToUser( "<username>", [ <roles> ], { <writeConcern> } ) The
db.grantRolesToUser()method takes the following arguments:Parameterタイプ説明userstring
ロールを付与するユーザーの名前。
roles配列
ユーザーに付与する追加のロールの配列。
writeConcernドキュメント
任意。 操作の 書込み保証( write concern ) のレベル。 詳しくは、 書込み保証(write concern) の仕様を参照してください。
rolesフィールドでは、組み込みロールとユーザー定義ロールの両方を指定できます。To specify a role that exists in the same database where
db.grantRolesToUser()runs, you can either specify the role with the name of the role:"readWrite" または、次のように、ドキュメントを使用してロールを指定することもできます。
{ role: "<role>", db: "<database>" } 別のデータベースに存在するロールを指定するには、 ドキュメントを使用してロールを指定します。
The
db.grantRolesToUser()method wraps thegrantRolesToUsercommand.
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
重要
このコマンドは、MongoDB Atlas クラスターではサポートされていません。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
動作
レプリカセット
If run on a replica set, db.grantRolesToUser() is executed using "majority" write concern by default.
必要なアクセス権
例
products データベースに次のロールを持つユーザー accountUser01 がある場合:
"roles" : [ { "role" : "assetsReader", "db" : "assets" } ]
The following db.grantRolesToUser() operation gives accountUser01 the readWrite role on the products database and the read role on the stock database.
use products db.grantRolesToUser( "accountUser01", [ "readWrite" , { role: "read", db: "stock" } ], { w: "majority" , wtimeout: 4000 } )
productsデータベース内のユーザー accountUser01 には現在、次のロールがあります。
"roles" : [ { "role" : "assetsReader", "db" : "assets" }, { "role" : "read", "db" : "stock" }, { "role" : "readWrite", "db" : "products" } ]