usersInfo
On this page本页内容
Definition定义
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 | usersInfo has multiple forms depending on the requested information. usersInfo的参数有多种形式,具体取决于请求的信息。usersInfo: <various>. usersInfo: <various>。 |
showCredentials | boolean | true to display the user's password hash.true可显示用户的密码哈希。false. false。 |
showCustomData | boolean | false to omit the user's customData from the output.false可从输出中省略用户的customData。true. true。 |
showPrivileges | boolean | true to show the user's full set of privileges, including expanded information for the inherited roles.true可显示用户的全套权限,包括继承角色的扩展信息。false.false。 |
showAuthenticationRestrictions | boolean | true to show the user's authentication restrictions.true以显示用户的身份验证限制。false.false。 |
filter | document | $match stage conditions to return information for users that match the filter conditions.$match阶段条件以返回与筛选条件匹配的用户信息的文档。 |
comment | any |
|
usersInfo: <various>
{ usersInfo: <various> }
The argument to usersInfo has multiple forms depending on the requested information:usersInfo的参数有多种形式,具体取决于请求的信息:
| Argument | Returns |
|---|---|
{ usersInfo: 1 } | mongoshdb.getUsers() helper for this invocation of the command. db.getUsers()帮助程序。 |
{ usersInfo: <username> } | mongoshdb.getUser() helper for this invocation of the command. db.getUser()帮助程序。 |
{ 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. showPrivileges和showCredentials。For example:例如:
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:showCustomData设置为false的情况下运行usersInfo:
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
}