Docs 主页 → 开发应用程序 → MongoDB Manual
usersInfo
定义
usersInfo
返回一个或多个用户的信息。
usersInfo
命令采用以下形式:{ usersInfo: <various>, showCredentials: <Boolean>, showPrivileges: <Boolean>, showAuthenticationRestrictions: <Boolean>, filter: <document>, comment: <any> } 该命令具有以下字段:
字段类型说明usersInfo
多个要返回信息的用户。
usersInfo
的参数有多种形式,具体取决于请求的信息。请参阅usersInfo: <various>
。showCredentials
布尔可选。 将该字段设置为 true 以显示用户的密码哈希值。 默认情况下,此字段为false
。showPrivileges
布尔可选。设置为
true
以显示用户的完整权限集,包括继承角色的扩展信息。默认情况下,此字段为
false
。如果查看所有用户,则不能指定此字段。
showAuthenticationRestrictions
布尔可选。设置为
true
以显示用户的身份验证限制。默认情况下,此字段为
false
。如果查看所有用户,则不能指定此字段。
filter
文档可选。指定$match
阶段条件的文档,用于为符合过滤条件的用户返回信息。comment
注意到可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
mongod 日志消息,位于
attr.command.cursor.comment
字段中。command.comment
字段中的数据库分析器输出。currentOp
输出,在command.comment
字段。
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。
usersInfo: <various>
{ usersInfo: <various> }
usersInfo
的参数有多种形式,具体取决于请求的信息:
参数 | 返回: |
---|---|
{ usersInfo: 1 } | 返回有关运行命令的数据库中的用户的信息。
|
{ usersInfo: <username> } | 返回运行命令的数据库中存在的特定用户的信息。
|
{ usersInfo: { user: <name>, db: <db> } } | 返回有关由名称和数据库指定的用户的信息。 |
{ usersInfo: [ { user: <name>, db: <db> }, ... ] } { usersInfo: [ <username1>, ... ] } | 返回指定用户的信息。 |
{ forAllDBs: true } | 返回所有数据库中的用户信息。 |
必需的访问权限
用户可以随时查看自己的信息。
要查看其他用户的信息,运行该命令的用户必须具有包括对其他用户数据库执行 viewUser
操作的权限。
输出
根据指定的选项, 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 }
举例
查看特定用户
要查看 "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
选项。
查看数据库中符合指定过滤器的所有用户
usersInfo
命令可以接受filter
文档,以返回符合过滤条件的用户信息。
要查看当前数据库中具有指定角色的所有用户,请使用类似于以下内容的命令文档:
db.runCommand( { usersInfo: 1, filter: { roles: { role: "root", db: "admin" } } } )
查看所有用户时,可以指定 showCredentials
选项,但不能指定 showPrivileges
或 showAuthenticationRestrictions
选项。
查看具有SCRAM-SHA-1
档案的所有用户
usersInfo
命令可以接受filter
文档,以返回符合过滤条件的用户信息。
以下操作返回具有 SCRAM-SHA-1
凭证的所有用户。具体而言,该命令返回所有数据库中的所有用户,然后使用 $match
阶段将指定的过滤器应用于用户。
db.runCommand( { usersInfo: { forAllDBs: true}, filter: { mechanisms: "SCRAM-SHA-1" } } )
查看所有用户时,可以指定 showCredentials
选项,但不能指定 showPrivileges
或 showAuthenticationRestrictions
选项。