Database Manual / Reference / mongosh Methods / User Management

db.getUsers() (mongosh method方法)

Definition定义

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>
} )
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中新增。

filterdocument文档Optional. 可选。A document that specifies $match stage conditions to return information for users that match the filter conditions.一个文档,指定$match阶段条件,为符合筛选条件的用户返回信息。

For more information, see usersInfo.有关更多信息,请参阅usersInfo

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

Important

This command is not supported in MongoDB Atlas clusters. MongoDB Atlas集群不支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

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示例

View All Users for a Database that Match the Specified Filter查看与指定筛选器匹配的数据库的所有用户

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选项,但不能指定showPrivilegesshowAuthenticationRestrictions选项。

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

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:要检索用户但从输出中省略自定义数据,请运行db.getUsers()并将showCustomData设置为false

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
}