定义
db.getUsers(<options>)返回数据库中所有用户的信息。
db.getUsers()wraps theusersInfo: 1command.The
db.getUsers()method can take the following options:db.getUsers( { showCredentials: <Boolean>, showCustomData: <Boolean>, filter: <document> } ) 字段类型说明showCredentials布尔
可选。设置为
true,显示用户的密码哈希值。默认情况下,此字段为
false。showCustomData布尔
可选。设置为
false将省略输出中用户的customData。默认情况下,此字段为
true。5.2版本新增。
filter文档
可选。指定
$match阶段条件的文档,用于为符合过滤条件的用户返回信息。有关更多信息,请参阅
usersInfo。
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
必需的访问权限
要查看其他用户的信息,必须对其他用户的数据库执行 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 选项。
从输出中省略自定义数据
New in version 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 }