Docs Menu

updateRole

updateRole

Updates a user-defined role. The updateRole command must run on the role's database.

Tip

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

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

An update to a field completely replaces the previous field's values. To grant or remove roles or privileges without replacing all values, use one or more of the following commands:

警告

An update to the privileges or roles array completely replaces the previous array's values.

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

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

重要

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

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

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

To update a role, you must provide the privileges array, roles array, or both.

コマンドは、次の構文を使用します。

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

このコマンドは、次のフィールドを使用します。

フィールド
タイプ
説明

updateRole

string

The name of the user-defined role role to update.

privileges

配列

Optional. Required if you do not specify roles array. The privileges to grant the role. An update to the privileges array overrides the previous array's values. For the syntax for specifying a privilege, see the privileges array.

roles

配列

Optional. Required if you do not specify privileges array. The roles from which this role inherits privileges. An update to the roles array overrides the previous array's values.

authenticationRestrictions

配列

任意。

The authentication restrictions the server enforces on the role. Specifies a list of IP addresses and CIDR ranges users granted this role are allowed to connect to and/or which they can connect from.

writeConcern

ドキュメント

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

comment

any

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

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

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

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

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

A role's privileges apply to the database where the role is created. The role can inherit privileges from other roles in its database. A role created on the admin database can include privileges that apply to all databases or to the クラスター and can inherit privileges from roles in other databases.

You must have the revokeRole アクション on all databases in order to update a role.

You must have the grantRole アクション on the database of each role in the roles array to update the array.

You must have the grantRole アクション on the database of each privilege in the privileges array to update the array. If a privilege's resource spans databases, you must have grantRole on the admin database. A privilege spans databases if the privilege is any of the following:

  • a collection in all databases

  • all collections and all database

  • the cluster resource

You must have the setAuthenticationRestriction アクション on the database of the target role to update a role's authenticationRestrictions document.

The following is an example of the updateRole command that updates the myClusterwideAdmin role on the admin database. While the privileges and the roles arrays are both optional, at least one of the two is required:

db.adminCommand(
{
updateRole: "myClusterwideAdmin",
privileges:
[
{
resource: { db: "", collection: "" },
actions: [ "find" , "update", "insert", "remove" ]
}
],
roles:
[
{ role: "dbAdminAnyDatabase", db: "admin" }
],
writeConcern: { w: "majority" }
}
)

To view a role's privileges, use the rolesInfo command.