Database Manual / Reference / Database Commands / User Management

usersInfo (database command数据库命令)

Definition定义

usersInfo
Returns information about one or more users.返回有关一个或多个用户的信息。

Compatibility兼容性

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command has limited support in M0 and Flex clusters. For more information, see Unsupported Commands.此命令在M0和Flex集群中的支持有限。有关详细信息,请参阅不支持的命令

  • 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的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

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

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
usersInfovarious

The user(s) about whom to return information.返回信息的用户。

The argument to usersInfo has multiple forms depending on the requested information. usersInfo的参数有多种形式,具体取决于所请求的信息。See usersInfo: <various>.请参阅usersInfo: <various>

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

showPrivilegesboolean布尔值

Optional. 可选。Set to true to show the user's full set of privileges, including expanded information for the inherited roles.设置为true可显示用户的全部权限,包括继承角色的扩展信息。

By default, this field is false.默认情况下,此字段为false

If viewing all users, you cannot specify this field.如果查看所有用户,则无法指定此字段。

showAuthenticationRestrictionsboolean布尔值

Optional. 可选。Set to true to show the user's authentication restrictions.设置为true以显示用户的身份验证限制。

By default, this field is false.默认情况下,此字段为false

If viewing all users, you cannot specify this field.如果查看所有用户,则无法指定此字段。

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

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

usersInfo: <various>

{ usersInfo: <various> }

The argument to usersInfo has multiple forms depending on the requested information:usersInfo的参数有多种形式,具体取决于所请求的信息:

Argument参数Returns返回
{ usersInfo: 1 }

Returns information about the users in the database where the command is run.返回运行命令的数据库中用户的信息。

mongosh provides the db.getUsers() helper for this invocation of the command.为该命令的调用提供db.getUsers()助手。

{ usersInfo: <username> }

Return information about the a specific user that exists in the database where the command is run.返回运行命令的数据库中存在的特定用户的信息。

mongosh provides the db.getUser() helper for this invocation of the command.为该命令的调用提供db.getUser()帮助程序。

{ usersInfo: { user: <name>, db: <db> } }Returns information about the user specified by the name and database.返回由名称和数据库指定的用户信息。
{ usersInfo: [ { user: <name>, db: <db> }, ... ] }
{ usersInfo: [ <username1>, ... ] }
Returns information about the specified users.返回有关指定用户的信息。
{ forAllDBs: true }Returns information about users in all databases.返回所有数据库中用户的信息。

Required Access所需访问权限

Users can always view their own information.用户始终可以查看自己的信息。

To view another user's information, the user running the command must have privileges that include the viewUser action on the other user's database.要查看其他用户的信息,运行该命令的用户必须具有权限,包括对其他用户的数据库执行viewUser操作。

Output输出

The following information can be returned by the usersInfo depending on the options specified:根据指定的选项,usersInfo可以返回以下信息:

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

Examples示例

View Specific Users查看特定用户

To see information and privileges, but not the credentials, for the user "Kari" defined in "home" database, run the following command:要查看"home"数据库中定义的用户"Kari"的信息和权限,但不查看凭据,请运行以下命令:

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

To view a user that exists in the current database, you can specify the user by name only. For example, if you are in the home database and a user named "Kari" exists in the home database, you can run the following command:要查看当前数据库中存在的用户,只能按名称指定用户。例如,如果您在home数据库中,并且home数据库中存在名为"Kari"的用户,则可以运行以下命令:

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

View Multiple Users查看多个用户

To view info for several users, use an array, with or without the optional fields showPrivileges and showCredentials. For example:要查看多个用户的信息,请使用数组,可以带或不带可选字段showPrivilegesshowCredentials。例如:

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

View All Users for a Database查看数据库的所有用户

To view all users on the database the command is run, use a command document that resembles the following:要查看运行命令的数据库上的所有用户,请使用类似于以下内容的命令文档:

db.runCommand( { usersInfo: 1 } )

When viewing all users, you can specify the showCredentials option but not the showPrivileges or the showAuthenticationRestrictions options.查看所有用户时,可以指定showCredentials选项,但不能指定showPrivilegesshowAuthenticationRestrictions选项。

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

The usersInfo command can accept a filter document to return information for users that match the filter condition.usersInfo命令可以接受filter文档,为符合筛选条件的用户返回信息。

To view all users in the current database who have the specified role, use a command document that resembles the following:要查看当前数据库中具有指定角色的所有用户,请使用类似于以下内容的命令文档:

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

When viewing all users, you can specify the showCredentials option but not the showPrivileges or the showAuthenticationRestrictions options.查看所有用户时,可以指定showCredentials选项,但不能指定showPrivilegesshowAuthenticationRestrictions选项。

View All Users with SCRAM-SHA-1 Credentials查看所有拥有SCRAM-SHA-1凭据的用户

The usersInfo command can accept a filter document to return information for users that match the filter condition.usersInfo命令可以接受筛选文档,为符合filter条件的用户返回信息。

The following operation returns all users that have SCRAM-SHA-1 credentials. Specifically, the command returns all users across all databases and then uses the $match stage to apply the specified filter to the users.以下操作返回具有SCRAM-SHA-1凭据的所有用户。具体来说,该命令返回所有数据库中的所有用户,然后使用$match阶段将指定的筛选器应用于用户。

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

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 usersInfo output, set the showCustomData option to false.要从usersInfo输出中省略用户的自定义数据,请将showCustomData选项设置为false

Use the createUser command to create a user named accountAdmin01 on the products database:使用createUser命令在products数据库上创建名为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 usersInfo with showCustomData set to false:要检索用户但从输出中省略自定义数据,请运行usersInfo并将showCustomData设置为false

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