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

usersInfo(数据库命令)

usersInfo

返回一个或多个用户的信息。

此命令可用于以下环境中托管的部署:

重要

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

该命令具有以下语法:

db.runCommand(
{
usersInfo: <various>,
showCredentials: <Boolean>,
showCustomData: <Boolean>,
showPrivileges: <Boolean>,
showAuthenticationRestrictions: <Boolean>,
authenticatedMechanism: <string>,
filter: <document>,
comment: <any>
}
)

该命令接受以下字段:

字段
类型
说明

usersInfo

多个

要返回信息的用户。

The argument to usersInfo has multiple forms depending on the requested information. See usersInfo: <various>.

showCredentials

布尔

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

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

showCustomData

布尔

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

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

5.2版本新增。

showPrivileges

布尔

可选。设置为 true 以显示用户的完整权限集,包括继承角色的扩展信息。

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

如果查看所有用户,则不能指定此字段。

showAuthenticationRestrictions

布尔

可选。设置为 true 以显示用户的身份验证限制。

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

如果查看所有用户,则不能指定此字段。

authenticatedMechanism

字符串

9.0版本新增。

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

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

如果查看所有用户,则不能指定此字段。

filter

文档

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

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

{ usersInfo: <various> }

usersInfo 的参数有多种形式,具体取决于请求的信息:

Argument
返回:

{ usersInfo: 1 }

返回有关运行命令的数据库中的用户的信息。

mongosh provides the db.getUsers() helper for this invocation of the command.

{ usersInfo: <username> }

返回运行命令的数据库中存在的特定用户的信息。

mongosh provides the db.getUser() helper for this invocation of the command.

{ usersInfo: { user: <name>, db: <db> } }

返回有关由名称和数据库指定的用户的信息。

{ usersInfo: [ { user: <name>, db: <db> }, ... ] }
{ usersInfo: [ <username1>, ... ] }

返回指定用户的信息。

{ forAllDBs: true }

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

用户可以随时查看自己的信息。

要查看其他用户的信息,运行该命令的用户必须具有包括对其他用户数据库执行 viewUser 操作的权限。

The following information can be returned by the usersInfo depending on the options specified:

{
"users" : [
{
"_id" : "<db>.<username>",
"userId" : <UUID>,
"user" : "<username>",
"db" : "<db>",
"mechanisms" : [ ... ],
"customData" : <document>,
"roles" : [ ... ],
"credentials": { ... }, // only if showCredentials: true
"inheritedRoles" : [ ... ], // only if showPrivileges: true or showAuthenticationRestrictions: true
"inheritedPrivileges" : [ ... ], // only if showPrivileges: true or showAuthenticationRestrictions: true
"inheritedAuthenticationRestrictions" : [ ] // only if showPrivileges: true or showAuthenticationRestrictions: true
"authenticationRestrictions" : [ ... ] // only if showAuthenticationRestrictions: true
},
...
],
"ok" : 1
}

要查看 "home" 数据库中定义的用户 "Kari" 的信息和权限,但不查看凭证,请运行以下命令:

db.runCommand(
{
usersInfo: { user: "Kari", db: "home" },
showPrivileges: true
}
)

要查看当前数据库中存在的用户,可以只通过名称指定用户。例如,如果您位于 home 数据库中,并且 home 数据库中存在名为 "Kari" 的用户,则可以运行以下命令:

db.getSiblingDB("home").runCommand(
{
usersInfo: "Kari",
showPrivileges: true
}
)

要查看多个用户的信息,请使用数组,带或不带可选字段 showPrivileges 和 showCredentials。例如:

db.runCommand( {
usersInfo: [ { user: "Kari", db: "home" }, { user: "Li", db: "myApp" } ],
showPrivileges: true
} )

要查看运行命令的数据库中的所有用户,请使用类似下面的命令文档:

db.runCommand( { usersInfo: 1 } )

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

The usersInfo command can accept a filter document to return information for users that match the filter condition.

要查看当前数据库中具有指定角色的所有用户,请使用类似于以下内容的命令文档:

db.runCommand( { usersInfo: 1, filter: { roles: { role: "root", db: "admin" } } } )

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

The usersInfo command can accept a filter document to return information for users that match the filter condition.

以下操作返回具有 SCRAM-SHA-1 凭证的所有用户。具体而言,该命令返回所有数据库中的所有用户,然后使用 $match 阶段将指定的过滤器应用于用户。

db.runCommand( { usersInfo: { forAllDBs: true}, filter: { mechanisms: "SCRAM-SHA-1" } } )

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

5.2 版本中的新增功能:

To omit users' custom data from the usersInfo 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 usersInfo with showCustomData set to false:

db.getSiblingDB("products").runCommand ( {
usersInfo: "accountAdmin01",
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
}