Definition定义
usersInfoReturns 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:该命令包含以下字段:
usersInfo | various |
|
showCredentials |
| |
showCustomData |
| |
showPrivileges |
| |
showAuthenticationRestrictions |
| |
filter | $match stage conditions to return information for users that match the filter conditions.$match阶段条件,为符合筛选条件的用户返回信息。 | |
comment |
|
usersInfo: <various>
{ usersInfo: <various> }
The argument to usersInfo has multiple forms depending on the requested information:usersInfo的参数有多种形式,具体取决于所请求的信息:
{ usersInfo: 1 } |
|
{ usersInfo: <username> } |
|
{ usersInfo: { user: <name>, db: <db> } } | |
{ usersInfo: [ { user: <name>, db: <db> }, ... ] }{ usersInfo: [ <username1>, ... ] } | |
{ forAllDBs: true } |
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:showPrivileges和showCredentials。例如:
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选项,但不能指定showPrivileges或showAuthenticationRestrictions选项。
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选项,但不能指定showPrivileges或showAuthenticationRestrictions选项。
View All Users with SCRAM-SHA-1 Credentials查看所有拥有SCRAM-SHA-1凭据的用户
SCRAM-SHA-1 CredentialsThe 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选项,但不能指定showPrivileges或showAuthenticationRestrictions选项。
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
}