对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db.getUsers()(mongosh方法)

db.getUsers(<options>)

返回数据库中所有用户的信息。

db.getUsers() wraps the usersInfo: 1 command.

The db.getUsers() method can take the following options:

db.getUsers( {
showCredentials: <Boolean>,
showCustomData: <Boolean>,
authenticatedMechanism: <string>,
filter: <document>
} )
字段
类型
说明

showCredentials

布尔

可选。设置为 true,显示用户的密码哈希值。

默认情况下,此字段为 false。

showCustomData

布尔

可选。设置为 false 将省略输出中用户的 customData。

默认情况下,此字段为 true。

5.2版本新增。

authenticatedMechanism

字符串

9.0版本新增。

可选。设置为表示受支持的身份验证机制的 string,以确定 db.getUsers() 在何处查找用户授权信息。有关机制及其相关 string 的列表,请参阅 authenticationMechanisms。

如果未指定此字段,db.getUsers() 将返回 $external 数据库中定义的用户从 LDAP 获取的信息,以及 SCRAM 用户从本地授权数据库获取的信息。

filter

文档

可选。指定 $match 阶段条件的文档,用于为符合过滤条件的用户返回信息。

有关更多信息,请参阅 usersInfo。

此方法可用于以下环境中托管的部署:

重要

This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

要查看其他用户的信息,必须对其他用户的数据库执行 viewUser 操作。

用户可查看自己的信息。

The db.getUsers() method can accept a filter document to return information for users that match the filter condition.

要查看当前数据库中拥有 SCRAM-SHA-256 档案的所有用户:

db.getUsers({ filter: { mechanisms: "SCRAM-SHA-256" } })

查看所有用户时,可以指定 showCredentials 选项,但不能指定 showPrivileges 或 showAuthenticationRestrictions 选项。

5.2 版本中的新增功能:

To omit users' custom data from the db.getUsers() output, set the showCustomData option to false.

使用 createUser 命令在 products 数据库上创建名为 accountAdmin01 的用户:

db.getSiblingDB("products").runCommand( {
createUser: "accountAdmin01",
pwd: passwordPrompt(),
customData: { employeeId: 12345 },
roles: [ { role: 'readWrite', db: 'products' } ]
} )

用户包含一个 customData 字段 { employeeId: 12345 }。

To retrieve the user but omit the custom data from the output, run db.getUsers() with showCustomData set to false:

db.getSiblingDB("products").getUsers( { showCustomData: false } )

示例输出:

{
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
}