Docs Menu

createRole

createRole

Creates a role and specifies its privileges. The role applies to the database on which you run the command. The createRole command returns a duplicate role error if the role already exists in the database.

Tip

mongoshでは、このコマンドはdb.createRole()ヘルパー メソッドを通じて実行することもできます。

ヘルパー メソッドはmongoshユーザーには便利ですが、データベースコマンドと同じレベルの情報は返されない可能性があります。 便宜上必要ない場合、または追加の戻りフィールドが必要な場合は、 データベースコマンドを使用します。

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

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

重要

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

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

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

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

db.adminCommand(
{
createRole: "<new role>",
privileges: [
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>" | "<CIDR range>", ...]
},
...
],
writeConcern: <write concern document>,
comment: <any>
}
)

The createRole command has the following fields:

フィールド
タイプ
説明

createRole

string

The name of the new role.

privileges

配列

The privileges to grant the role. A privilege consists of a resource and permitted actions. For the syntax of a privilege, see the privileges array.

You must include the privileges field. Use an empty array to specify no privileges.

roles

配列

An array of roles from which this role inherits privileges.

You must include the roles field. Use an empty array to specify no roles to inherit from.

authenticationRestrictions

配列

任意。

サーバーがロールに強制する認証制限。このロールを付与されたユーザーが接続できる、または接続元として使用できる IP アドレスと CIDR 範囲のリストを指定します。

writeConcern

ドキュメント

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

comment

any

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

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

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

createRoleが実行されるのと同じデータベースに存在するロールを指定するには、ロールの名前を使用してロールを指定します。

"readWrite"

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

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

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

authenticationRestrictionsドキュメントには、次のフィールドのみを含めることができます。 authenticationRestrictionsドキュメントに認識されないフィールドが含まれている場合、サーバーはエラーをスローします。

フィールド名
説明

clientSource

IP アドレスおよび/またはCIDR範囲の配列

存在する場合、サーバーはユーザーを認証する際に、クライアントの IP アドレスが指定されたリストに含まれているか、リスト内の CIDR 範囲に属していることを確認します。 クライアントの IP アドレスが存在しない場合、サーバーはユーザーを認証しません。

serverAddress

IP アドレスおよび/またはCIDR範囲の配列

クライアントが接続できる IP アドレスまたは CIDR 範囲のリスト。 存在する場合、サーバーはクライアントの接続が指定されたリスト内の IP アドレス経由で受け入れられたことを確認します。 認識されない IP アドレス経由で接続が受け入れられた場合、サーバーはユーザーを認証しません。

重要

ユーザーが互換性のない認証制限を持つ複数のロールを継承した場合、そのユーザーは使用できなくなります。

たとえば、ユーザーが、 clientSourceフィールドが["198.51.100.0"]であるロールと、 clientSourceフィールドが["203.0.113.0"]である別のロールを継承した場合、サーバーはユーザーを認証できません。

MongoDB での認証の詳細については、「自己管理型配置での認証 」を参照してください。

ロールの特権は、ロールが作成されたデータベースに適用されます。 ロールはデータベース内の他のロールから特権を継承することができます。 adminデータベースで作成されたロールには、すべてのデータベースまたはクラスターに適用する特権を含めることができ、他のデータベースのロールから特権を継承できます。

To create a role in a database, you must have:

Built-in roles userAdmin and userAdminAnyDatabase provide createRole and grantRole actions on their respective resources.

To create a role with authenticationRestrictions specified, you must have the setAuthenticationRestriction アクション on the database resource which the role is created.

The following createRole command creates the myClusterwideAdmin role on the admin database:

db.adminCommand({ createRole: "myClusterwideAdmin",
privileges: [
{ resource: { cluster: true }, actions: [ "addShard" ] },
{ resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
{ resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
{ resource: { db: "", collection: "" }, actions: [ "find" ] }
],
roles: [
{ role: "read", db: "admin" }
],
writeConcern: { w: "majority" , wtimeout: 5000 }
})