On this page本页内容
db.getUsers(<options>)
Returns information for all the users in the database.返回数据库中所有用户的信息。
db.getUsers() wraps the 包装了usersInfo: 1 command.usersInfo: 1命令。
The db.getUsers() method can take the following options:db.getUsers()方法可以采用以下选项:
db.getUsers( {
showCredentials: <Boolean>,
showCustomData: <Boolean>,
filter: <document>
} )
showCredentials | boolean |
|
showCustomData | boolean |
|
filter | document |
|
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.用户可以查看自己的信息。
New in version 4.0.在版本4.0中新增。The db.getUsers() method can accept a filter document to return information for users that match the filter condition.db.getUsers()方法可以接受filter文档,以返回与筛选器条件匹配的用户的信息。
To view all users for the current database who have 要查看当前数据库中拥有SCRAM-SHA-256 credentials:SCRAM-SHA-256凭据的所有用户:
db.getUsers({ filter: { mechanisms: "SCRAM-SHA-256" } })
When viewing all users, you can specify the 查看所有用户时,可以指定showCredentials option but not the showPrivileges or the showAuthenticationRestrictions options.showCredentials选项,但不能指定showPrivileges或showAuthenticationRestrictions选项。
New in version 5.2.在版本5.2中新增。To omit users' custom data from the 要从db.getUsers() output, set the showCustomData option to false.db.getUsers()输出中省略用户的自定义数据,请将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.getUsers() with showCustomData set to false:showCustomData设置为false的情况下运行db.getUsers():
db.getSiblingDB("products").getUsers( { showCustomData: false } )
Example output:示例输出:
{
users: [
{
_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' ]
}
],
ok: 1
}