db.getRoles()

On this page本页内容

Definition定义

db.getRoles()

Returns information for all the roles in the database on which the command runs. 返回运行命令的数据库中所有角色的信息。The method can be run with or without an argument.该方法可以在有或没有参数的情况下运行。

If run without an argument, db.getRoles() returns inheritance information for the database's user-defined roles.如果不带参数运行,db.getRoles()将返回数据库用户定义角色的继承信息。

To return more information, pass the db.getRoles() a document with the following fields:要返回更多信息,请向db.getRoles()传递带有以下字段的文档:

Field字段Type类型Description描述
rolesInfointegerSet this field to 1 to retrieve all user-defined roles.将此字段设置为1可检索所有用户定义的角色。
showPrivilegesbooleanOptional. 可选。Set the field to true to show role privileges, including both privileges inherited from other roles and privileges defined directly. 将该字段设置为true以显示角色权限,包括从其他角色继承的权限和直接定义的权限。By default, the command returns only the roles from which this role inherits privileges and does not return specific privileges.默认情况下,该命令仅返回此角色继承的权限的角色,而不返回特定权限。
showBuiltinRolesbooleanOptional. 可选。Set to true to display built-in roles as well as user-defined roles.设置为true可显示内置角色和用户定义的角色。

db.getRoles() wraps the rolesInfo command.包装了rolesInfo命令。

Required Access所需访问权限

To view a role's information, you must be either explicitly granted the role or must have the viewRole action on the role's database.要查看角色的信息,必须明确授予您该角色,或者必须对角色的数据库执行viewRole操作

Example示例

The following operations return documents for all the roles on the products database, including role privileges and built-in roles:以下操作将返回products数据库中所有角色的文档,包括角色权限和内置角色:

db.getRoles(
    {
      rolesInfo: 1,
      showPrivileges:true,
      showBuiltinRoles: true
    }
)
←  db.getRole()db.grantPrivilegesToRole() →