AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

ユーザーInfo(データベースコマンド)

usersInfo

1 人以上のユーザーに関する情報を返します。

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

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

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

重要

This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

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

db.runCommand(
{
usersInfo: <various>,
showCredentials: <Boolean>,
showCustomData: <Boolean>,
showPrivileges: <Boolean>,
showAuthenticationRestrictions: <Boolean>,
authenticatedMechanism: <string>,
filter: <document>,
comment: <any>
}
)

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

フィールド
タイプ
説明

usersInfo

さまざまな

情報を返すユーザーに関するユーザー。

The argument to usersInfo has multiple forms depending on the requested information. See usersInfo: <various>.

showCredentials

ブール値

任意。 ユーザーのパスワード ハッシュを表示するには、 をtrueに設定します。

デフォルトでは、このフィールドは false です。

showCustomData

ブール値

任意。 Set to false to omit the user's customData from the output.

デフォルトでは、このフィールドは true です。

バージョン5.2の新機能。

showPrivileges

ブール値

任意。 をtrueに設定すると、継承されたロールの展開情報を含むユーザーの完全な権限セットが表示されます。

デフォルトでは、このフィールドは false です。

すべてのユーザーを表示する場合は、このフィールドを指定できません。

showAuthenticationRestrictions

ブール値

任意。 ユーザーの認証制限を表示するには、 trueに設定します。

デフォルトでは、このフィールドは false です。

すべてのユーザーを表示する場合は、このフィールドを指定できません。

authenticatedMechanism

string

バージョン9.0の新機能。

任意。サポートされている認証メカニズムを表すstringに設定し、usersInfoがユーザーの承認情報をどこで検索するかを決定します。メカニズムとその関連stringのリストについては、authenticationMechanismsを参照してください。

このフィールドが指定されていない場合、usersInfoは、$externalデータベースで定義されたユーザーについてはLDAPから、SCRAMユーザーについてはローカル承認データベースから情報を返します。

すべてのユーザーを表示する場合は、このフィールドを指定できません。

filter

ドキュメント

任意。 フィルター条件に一致するユーザーの情報を返すために$matchステージ条件を指定するドキュメント。

comment

any

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

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

{ usersInfo: <various> }

usersInfoの引数には、リクエストされた情報に応じて複数の形式があります。

Argument
戻り値

{ usersInfo: 1 }

コマンドが実行されたデータベース内のユーザーに関する情報を返します。

mongosh provides the db.getUsers() helper for this invocation of the command.

{ usersInfo: <username> }

コマンドが実行されたデータベースに存在する特定のユーザーに関する情報を返します。

mongosh provides the db.getUser() helper for this invocation of the command.

{ usersInfo: { user: <name>, db: <db> } }

名前とデータベースによって指定されたユーザーに関する情報を返します。

{ usersInfo: [ { user: <name>, db: <db> }, ... ] }
{ usersInfo: [ <username1>, ... ] }

指定したユーザーに関する情報を返します。

{ forAllDBs: true }

すべてのデータベース内のユーザーに関する情報を返します。

ユーザーはいつでも自分の情報を表示できます。

別のユーザーの情報を表示するには、コマンドを実行しているユーザーに、他のユーザーのデータベースに対するviewUserアクションを含む特権が必要です。

The following information can be returned by the usersInfo depending on the options specified:

{
"users" : [
{
"_id" : "<db>.<username>",
"userId" : <UUID>,
"user" : "<username>",
"db" : "<db>",
"mechanisms" : [ ... ],
"customData" : <document>,
"roles" : [ ... ],
"credentials": { ... }, // only if showCredentials: true
"inheritedRoles" : [ ... ], // only if showPrivileges: true or showAuthenticationRestrictions: true
"inheritedPrivileges" : [ ... ], // only if showPrivileges: true or showAuthenticationRestrictions: true
"inheritedAuthenticationRestrictions" : [ ] // only if showPrivileges: true or showAuthenticationRestrictions: true
"authenticationRestrictions" : [ ... ] // only if showAuthenticationRestrictions: true
},
...
],
"ok" : 1
}

"home"データベースで定義されているユーザー"Kari"の情報と特権を表示するには、次のコマンドを実行します。

db.runCommand(
{
usersInfo: { user: "Kari", db: "home" },
showPrivileges: true
}
)

現在のデータベースに存在するユーザーを表示するには、名前のみでユーザーを指定できます。 たとえば、 homeデータベースにいて、 "Kari"という名前のユーザーがhomeデータベースに存在する場合は、次のコマンドを実行できます。

db.getSiblingDB("home").runCommand(
{
usersInfo: "Kari",
showPrivileges: true
}
)

複数のユーザーの情報を表示するには、オプション フィールドshowPrivilegesとshowCredentialsの有無にかかわらず、配列を使用します。 例:

db.runCommand( {
usersInfo: [ { user: "Kari", db: "home" }, { user: "Li", db: "myApp" } ],
showPrivileges: true
} )

コマンドが実行されるデータベース上のすべてのユーザーを表示するには、次のようなコマンド ドキュメントを使用します。

db.runCommand( { usersInfo: 1 } )

すべてのユーザーを表示する場合は、 showCredentialsオプションを指定できますが、 showPrivilegesまたはshowAuthenticationRestrictionsオプションは指定できません。

The usersInfo command can accept a filter document to return information for users that match the filter condition.

現在のデータベース内で指定されたロールを持つすべてのユーザーを表示するには、次のようなコマンド ドキュメントを使用します。

db.runCommand( { usersInfo: 1, filter: { roles: { role: "root", db: "admin" } } } )

すべてのユーザーを表示する場合は、 showCredentialsオプションを指定できますが、 showPrivilegesまたはshowAuthenticationRestrictionsオプションは指定できません。

The usersInfo command can accept a filter document to return information for users that match the filter condition.

次の操作では、 SCRAM-SHA-1認証情報を持つすべてのユーザーが返されます。 具体的には、コマンドは全データベースのすべてのユーザーを返し、 $matchステージを使用して指定されたフィルターをユーザーに適用します。

db.runCommand( { usersInfo: { forAllDBs: true}, filter: { mechanisms: "SCRAM-SHA-1" } } )

すべてのユーザーを表示する場合は、 showCredentialsオプションを指定できますが、 showPrivilegesまたはshowAuthenticationRestrictionsオプションは指定できません。

バージョン 5.2 の新機能:

To omit users' custom data from the usersInfo output, set the showCustomData option to false.

createUserコマンドを使用して、 productsデータベースにaccountAdmin01という名前のユーザーを作成します。

db.getSiblingDB("products").runCommand( {
createUser: "accountAdmin01",
pwd: passwordPrompt(),
customData: { employeeId: 12345 },
roles: [ { role: 'readWrite', db: 'products' } ]
} )

ユーザーには{ employeeId: 12345 }のcustomDataフィールドが含まれています。

To retrieve the user but omit the custom data from the output, run usersInfo with showCustomData set to false:

db.getSiblingDB("products").runCommand ( {
usersInfo: "accountAdmin01",
showCustomData: false
} )

出力例:

{
users: [
{
_id: 'products.accountAdmin01',
userId: UUID("0955afc1-303c-4683-a029-8e17dd5501f4"),
user: 'accountAdmin01',
db: 'products',
roles: [ { role: 'readWrite', db: 'products' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
],
ok: 1
}