usersInfo

On this page本页内容

Definition定义

usersInfo

Returns information about one or more users.返回有关一个或多个用户的信息。

The usersInfo command has the following form:usersInfo命令的格式如下:

{
  usersInfo: <various>,
  showCredentials: <Boolean>,
  showCustomData: <Boolean>,
  showPrivileges: <Boolean>,
  showAuthenticationRestrictions: <Boolean>,
  filter: <document>,
  comment: <any>
}

The command has the following fields:该命令包含以下字段:

Field字段Type类型Description描述
usersInfovarious

The user(s) about whom to return information.关于向谁返回信息的用户。

The argument to usersInfo has multiple forms depending on the requested information. usersInfo的参数具有多种形式,具体取决于请求的信息。See usersInfo: <various>.请参阅usersInfo: <various>

showCredentialsboolean

Optional. 可选择的Set to true to display the user's password hash.设置为true以显示用户的密码哈希。

By default, this field is false.默认情况下,此字段为false

showCustomDataboolean

Optional. 可选择的Set to false to omit the user's customData from the output.设置为false可从输出中省略用户的customData

By default, this field is true.默认情况下,此字段为true

New in version 5.2.在版本5.2中新增

showPrivilegesboolean

Optional. 可选。Set to true to show the user's full set of privileges, including expanded information for the inherited roles.设置为true可显示用户的全部权限,包括继承角色的扩展信息。

By default, this field is false.默认情况下,此字段为false

If viewing all users, you cannot specify this field.如果查看所有用户,则不能指定此字段。

showAuthenticationRestrictionsboolean

Optional. 可选。Set to true to show the user's authentication restrictions.设置为true以显示用户的身份验证限制。

By default, this field is false.默认情况下,此字段为false

If viewing all users, you cannot specify this field.如果查看所有用户,则不能指定此字段。

filterdocument

Optional. 可选。A document that specifies $match stage conditions to return information for users that match the filter conditions.指定$match阶段条件的文档,用于返回与筛选条件匹配的用户的信息。

New in version 4.0.在版本4.0中新增

commentany

Optional. 可选。A user-provided comment to attach to this command. 用户提供了附加到此命令的注释。Once set, this comment appears alongside records of this command in the following locations:设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).

New in version 4.4.在版本4.4中新增

usersInfo: <various>

{ usersInfo: <various> }

The argument to usersInfo has multiple forms depending on the requested information:usersInfo的参数有多种形式,具体取决于请求的信息:

ArgumentReturns
{ usersInfo: 1 }

Returns information about the users in the database where the command is run.返回有关运行命令的数据库中用户的信息。

mongosh provides the db.getUsers() helper for this invocation of the command.为该命令的调用提供db.getUsers()帮助程序。

{ usersInfo: <username> }

Return information about the a specific user that exists in the database where the command is run.返回有关运行命令的数据库中存在的特定用户的信息。

mongosh provides the db.getUser() helper for this invocation of the command.为该命令的调用提供db.getUser()帮助程序。

{ usersInfo: { user: <name>, db: <db> } }Returns information about the user specified by the name and database.返回由名称和数据库指定的用户的信息。
{ usersInfo: [ { user: <name>, db: <db> }, ... ] }
{ usersInfo: [ <username1>, ... ] }
Returns information about the specified users.返回有关指定用户的信息。
{ forAllDBs: true }

Returns information about users in all databases.返回所有数据库中用户的信息。

New in version 4.0.在版本4.0中新增

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>,
        // Starting in MongoDB 4.0.9
         "user" : "<username>",
         "db" : "<db>",
         "mechanisms" : [ ... ],   // Starting in MongoDB 4.0
         "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数据库中,并且主数据库中存在名为"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. 要查看多个用户的信息,请使用一个数组,可以使用或不使用可选字段showPrivilegesshowCredentialsFor 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选项,但不能指定showPrivilegesshowAuthenticationRestrictions选项。

View All Users for a Database that Match the Specified Filter查看与指定筛选器匹配的数据库的所有用户

New in version 4.0.在版本4.0中新增 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选项,但不能指定showPrivilegesshowAuthenticationRestrictions选项。

View All Users with SCRAM-SHA-1 Credentials查看具有SCRAM-SHA-1凭据的所有用户

New in version 4.0.在版本4.0中新增 The 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. 以下操作将返回具有SCRAM-SHA-1凭据的所有用户。Specifically, the command returns all users across all databases and then uses the $match stage to apply the specified filter to the users.具体来说,该命令返回所有数据库中的所有用户,然后使用$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选项,但不能指定showPrivilegesshowAuthenticationRestrictions选项。

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命令在产品数据库上创建名为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
}
←  updateUserRole Management Commands →