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

db.getUser()(mongoshメソッド)

db.getUser(username, args)

Returns user information for a specified user. Run this method on the user's database. If the user doesn't exist in the database, db.getUser() returns null.

The db.getUser() method has the following parameters:

db.getUser( "<username>", {
showCredentials: <Boolean>,
showCustomData: <Boolean>,
showPrivileges: <Boolean>,
showAuthenticationRestrictions: <Boolean>,
filter: <document>
} )
Parameter
タイプ
説明

username

string

情報を検索するユーザーの名前。

args

ドキュメント

任意。 追加の引数を指定するドキュメント。

args ドキュメントは、次のフィールドをサポートします。

フィールド
タイプ
説明

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 です。

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

filter

ドキュメント

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

db.getUser() wraps the usersInfo: <username> command.

出力の詳細については、 usersInfo を参照してください。

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

重要

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

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

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

他のユーザーの情報を表示するには、他のユーザーのデータベースに対してviewUserアクションが必要です。

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

次の操作は、 accounts データベース内のサンプルappClient ユーザーに関する情報を返します。

use accounts
db.getUser("appClient")

出力例:

{
_id: 'accounts.appClient',
userId: UUID("1c2fc1bf-c4dc-4a22-8b04-3971349ce0dc"),
user: 'appClient',
db: 'accounts',
roles: [],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}

バージョン 5.2 の新機能:

To omit a user's custom data from the db.getUser() 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 db.getUser() with showCustomData set to false:

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

出力例:

{
_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' ]
}