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. 返回指定用户的用户信息。在用户的数据库上运行此方法。The user must exist on the database on which the method runs.该用户必须存在于运行该方法的数据库中。

The db.getUser() method has the following parameters:db.getUser()方法具有以下参数:

db.getUser( "<username>", {
   showCredentials: <Boolean>,
   showCustomData: <Boolean>,
   showPrivileges: <Boolean>,
   showAuthenticationRestrictions: <Boolean>,
   filter: <document>
} )
Parameter参数Type类型Description描述
usernamestringThe name of the user for which to retrieve information.要检索其信息的用户的名称。
argsdocumentOptional. 可选。A document specifying additional arguments.指定附加参数的文档。

The args document supports the following fields:args文档支持以下字段:

Field字段Type类型Description描述
showCredentialsboolean

Optional. 可选。Set to true to display the user's password hash.设置为true以显示用户的密码哈希。

By default, this field is false.默认情况下,此字段为false

showCustomDataboolean

Optional. 可选。Set to false to omit the user's customData from the output.设置为false可从输出中忽略用户的customData

By default, this field is true.默认情况下,此字段为true

New in version 5.2.在版本5.2中新增

showPrivilegesboolean

Optional. 可选。Set to true to show the user's full set of privileges, including expanded information for the inherited roles.设置为true可显示用户的完整权限集,包括继承角色的扩展信息。

By default, this field is false.默认情况下,此字段为false

If viewing all users, you cannot specify this field.如果查看所有用户,则无法指定此字段。

showAuthenticationRestrictionsboolean

Optional. 可选。Set to true to show the user's authentication restrictions.设置为true以显示用户的身份验证限制。

By default, this field is false.默认情况下,此字段为false

If viewing all users, you cannot specify this field.如果查看所有用户,则无法指定此字段。

filterdocument

Optional. 可选。A document that specifies $match stage conditions to return information for users that match the filter conditions.指定$match阶段条件的文档,以返回与筛选条件匹配的用户的信息。

New in version 4.0.在版本4.0中新增

db.getUser() wraps the usersInfo: <username> command.包装了usersInfo: <username>命令。

For details on output, see usersInfo.有关输出的详细信息,请参阅usersInfo

Required Access所需访问权限

To view another user's information, you must have the viewUser action on the other user's database.要查看其他用户的信息,必须对其他用户的数据库执行viewUser操作

Users can view their own information.用户可以查看自己的信息。

Examples示例

The following operations return information about an example appClient user in an accounts database:以下操作返回有关帐户数据库中示例appClient用户的信息:

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

Omit Custom Data from Output从输出中忽略自定义数据

New in version 5.2.在版本5.2中新增 To omit a user's custom data from the db.getUser() output, set the showCustomData option to false.要从db.getUser()输出中省略用户的自定义数据,请将showCustomData选项设置为false

Use the createUser command to create a user named accountAdmin01 on the products database:使用createUser命令在产品数据库上创建名为accountAdmin01的用户:

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 }.用户包含{ employeeId: 12345 }customData字段。

To retrieve the user but omit the custom data from the output, run db.getUser() with showCustomData set to false:要检索用户但忽略输出中的自定义数据,请在showCustomData设置为false的情况下运行db.getUser()

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

Example output:示例输出:

{
   _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' ]
}
←  db.dropAllUsers()db.getUsers() →