Docs 菜单

Docs 主页开发应用程序MongoDB Manual

usersInfo

在此页面上

  • 定义
  • 语法
  • 命令字段
  • usersInfo: <various>
  • 必需的访问权限
  • 输出
  • 举例
usersInfo

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

该命令具有以下语法:

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

该命令接受以下字段:

字段
类型
说明
usersInfo
多个

要返回信息的用户。

usersInfo的参数有多种形式,具体取决于请求的信息。请参阅 usersInfo: <various>

showCredentials
布尔

可选。设置为 true,显示用户的密码哈希值。

默认情况下,此字段为 false

showCustomData
布尔

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

默认情况下,此字段为 true

5.2 版本中的新增功能

showPrivileges
布尔

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

默认情况下,此字段为 false

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

showAuthenticationRestrictions
布尔

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

默认情况下,此字段为 false

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

filter
文档
可选。指定 $match 阶段条件的文档,用于为符合过滤条件的用户返回信息。
comment
注意到

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

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

{ usersInfo: <various> }

usersInfo 的参数有多种形式,具体取决于请求的信息:

参数
返回:
{ usersInfo: 1 }

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

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

{ usersInfo: <username> }

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

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

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

要查看多个用户的信息,请使用数组,带或不带可选字段 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
}
← updateUser