db.getUser()
On this page
Definition
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()
returnsnull
.The
db.getUser()
method has the following parameters:db.getUser( "<username>", { showCredentials: <Boolean>, showPrivileges: <Boolean>, showAuthenticationRestrictions: <Boolean>, filter: <document> } ) ParameterTypeDescriptionusername
stringThe name of the user for which to retrieve information.args
documentOptional. A document specifying additional arguments.The
args
document supports the following fields:FieldTypeDescriptionshowCredentials
booleanOptional. Set the field to true to display the user's password hash. By default, this field isfalse
.showPrivileges
booleanOptional. Set the field to true to show the user's full set of privileges, including expanded information for the inherited roles. By default, this field isfalse
. If viewing all users, you cannot specify this field.showAuthenticationRestrictions
booleanOptional. Set the field to true to show the user's authentication restrictions. By default, this field isfalse
. If viewing all users, you cannot specify this field.filter
documentOptional. A document that specifies$match
stage conditions to return information for users that match the filter conditions.db.getUser()
wraps theusersInfo: <username>
command.For details on output, see
usersInfo
.
Compatibility
This method is available in deployments hosted in the following environments:
Important
This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Required Access
To view another user's information, you must have the
viewUser
action on the
other user's database.
Users can view their own information.
Example
The following operations return information about an example
appClient
user in an accounts
database:
use accounts db.getUser("appClient")
Example output:
{ _id: 'accounts.appClient', userId: UUID("1c2fc1bf-c4dc-4a22-8b04-3971349ce0dc"), user: 'appClient', db: 'accounts', roles: [], mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ] }