文档菜单
文档首页
/
MongoDB 手册
/ / /

usersInfo

本页内容

  • 定义
  • 兼容性
  • 语法
  • 命令字段
  • usersInfo: <各种>
  • 所需访问
  • 输出
  • 示例
usersInfo

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

此命令在以下环境中托管的应用中可用

  • MongoDB Atlas:云中MongoDB部署的完全托管服务

重要

此命令在M0、M2和M5集群中支持有限。有关更多信息,请参阅不受支持的命令。

  • MongoDB Enterprise:基于订阅的MongoDB自托管版本

  • MongoDB Community:源代码开放、免费使用和自托管的MongoDB版本

该命令具有以下语法

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

该命令采用以下字段

字段
类型
描述
usersInfo
various

返回信息的用户。

usersInfo的参数根据所需信息有多种形式。请参阅usersInfo: <various>.

showCredentials
布尔值

可选。将设置为true以显示用户的密码散列。

默认情况下,此字段为false

showCustomData
布尔值

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

默认情况下,此字段为true

新功能版本5.2.

showPrivileges
布尔值

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

默认情况下,此字段为false

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

showAuthenticationRestrictions
布尔值

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

默认情况下,此字段为false

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

filter
document
可选。一个指定 $match 阶段条件的文档,以返回符合过滤条件用户的信息。
comment
any

可选。附加到此命令的用户提供的注释。一旦设置,此注释将出现在以下位置的此命令记录旁边

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

{ usersInfo: <various> }

usersInfo 的参数根据请求的信息有多种形式

参数
返回
{ usersInfo: 1 }

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

mongosh 为此命令调用提供了 db.getUsers() 助手。

{ usersInfo: <username> }

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

mongosh 为此命令调用提供了 db.getUser() 助手。

{ 用户信息: { 用户: <name>, 数据库: <db> } }
返回指定名称和数据库的用户信息。
{ 用户信息: [ { 用户: <name>, 数据库: <db> }, ... ] }
{ 用户信息: [ <username1>, ... ] }
返回指定用户的信息。
{ 对于所有数据库: 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
}

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

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

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

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

要查看多个用户的信息,请使用数组,可以包含或省略可选字段 showPrivilegesshowCredentials。例如

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

要查看在运行命令的数据库上的所有用户,请使用以下类似的命令文档

db.runCommand( { usersInfo: 1 } )

在查看所有用户时,您可以指定 showCredentials 选项,但不能指定 showPrivilegesshowAuthenticationRestrictions 选项。

usersInfo 命令可以接受一个 filter 文档来返回符合过滤器条件的用户信息。

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

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

在查看所有用户时,您可以指定 showCredentials 选项,但不能指定 showPrivilegesshowAuthenticationRestrictions 选项。

usersInfo 命令可以接受一个 filter 文档来返回符合过滤器条件的用户信息。

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

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

在查看所有用户时,您可以指定 showCredentials 选项,但不能指定 showPrivilegesshowAuthenticationRestrictions 选项。

新功能版本5.2: 要从 usersInfo 输出中省略用户的自定义数据,请将 showCustomData 选项设置为 false

使用 createUser 命令在 products 数据库中创建名为 accountAdmin01 的用户

db.getSiblingDB("products").runCommand( {
createUser: "accountAdmin01",
pwd: passwordPrompt(),
customData: { employeeId: 12345 },
roles: [ { role: 'readWrite', db: 'products' } ]
} )

该用户包含一个 customData 字段,内容为 { employeeId: 12345 }

要检索用户但省略输出中的自定义数据,请运行 usersInfo 并将 showCustomData 设置为 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
}

返回

更新用户