Docs Menu

db.createRole()

db.createRole(role, writeConcern)

Creates a role in a database. You can specify privileges for the role by explicitly listing the privileges or by having the role inherit privileges from other roles or both. The role applies to the database on which you run the method.

重要

mongosh メソッド

このページでは、mongosh メソッドについて記載しています。ただし、データベースコマンドや Node.js などの言語固有のドライバーのドキュメントには該当しません

データベースコマンドについては、createRole コマンドを参照してください。

MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。

The db.createRole() method accepts the following arguments:

Parameter
タイプ
説明

role

ドキュメント

A document containing the name of the role and the role definition.

writeConcern

ドキュメント

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

The role document has the following form:

{
role: "<name>",
privileges: [
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>" | "<CIDR range>", ...]
},
...
]
}

roleドキュメントには、以下のフィールドがあります。

フィールド
タイプ
説明

role

string

新しいロールの名前。

privileges

配列

ロールに付与する特権。特権は、リソースと許可されたアクションから構成されます。権限の構文については、privileges 配列を参照してください。

privileges フィールドを含める必要があります。特権を指定しない場合は、空の配列を使用します。

roles

配列

このロールが特権を継承するロールの配列。

roles フィールドを含める必要があります。継承するロールを指定しない場合は、空の配列を使用します。

authenticationRestrictions

配列

任意。

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

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

db.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 での認証の詳細については、「自己管理型配置での認証 」を参照してください。

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

重要

このコマンドは、 MongoDB Atlasクラスターではサポートされていません。 すべてのコマンドの Atlas サポートの詳細については、「 サポートされていないコマンド 」を参照してください。

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

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

レプリカセットで実行する場合、 db.createRole()はデフォルトで"majority"書込み保証を使用して実行されます。

Except for roles created in the admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database.

A role created in the admin database can include privileges that apply to the admin database, other databases or to the クラスター resource, and can inherit from roles in other databases as well as the admin database.

The db.createRole() method returns a duplicate role error if the role already exists in the database.

データベースにロールを作成するには、以下が必要です。

組み込みロール userAdminuserAdminAnyDatabase は、それぞれのリソース上で createRole および grantRole のアクションを提供します。

authenticationRestrictions を指定してロールを作成するには、ロールが作成されるデータベース リソース上のsetAuthenticationRestriction アクションが必要です。

The following db.createRole() method creates the myClusterwideAdmin role on the admin database:

use admin
db.createRole(
{
role: "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" }
]
},
{ w: "majority" , wtimeout: 5000 }
)