On this page本页内容
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> } )
username | string | |
args | document |
The args
document supports the following fields:args
文档支持以下字段:
showCredentials | boolean |
|
showCustomData | boolean |
|
showPrivileges | boolean |
|
showAuthenticationRestrictions | boolean |
|
filter | document |
|
db.getUser()
wraps the 包装了usersInfo: <username>
command.usersInfo: <username>
命令。
For details on output, see 有关输出的详细信息,请参阅usersInfo
.usersInfo
。
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.用户可以查看自己的信息。
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' ] }
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' ] }