Docs Menu

db.getUser()

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.

db.getUser()メソッドには次のパラメータがあります。

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

username

string

The name of the user for which to retrieve information.

args

ドキュメント

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

The args document supports the following fields:

フィールド
タイプ
説明

showCredentials

ブール値

Optional. Set to true to display the user's password hash.

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

showCustomData

ブール値

Optional. Set to false to omit the user's customData from the output.

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

バージョン 5.2 で追加

showPrivileges

ブール値

Optional. Set to true to show the user's full set of privileges, including expanded information for the inherited roles.

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

If viewing all users, you cannot specify this field.

showAuthenticationRestrictions

ブール値

Optional. Set to true to show the user's authentication restrictions.

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

If viewing all users, you cannot specify this field.

filter

ドキュメント

Optional. A document that specifies $match stage conditions to return information for users that match the filter conditions.

db.getUser()usersInfo: <username>コマンドをラップします。

For details on output, see usersInfo.

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

重要

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

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

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

To view another user's information, you must have the viewUser アクション on the other user's database.

Users can view their own information.

The following operations return information about an example appClient user in an accounts database:

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.

Use the createUser command to create a user named accountAdmin01 on the products database:

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

The user contains a customData field of { employeeId: 12345 }.

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