db.getRoles()

Definition

  • db.getRoles()
  • Returns information for all the roles in the database on which the commandruns. The method can be run with or without an argument.

If run without an argument, db.getRoles() returns inheritanceinformation for the database’s user-definedroles.

To return more information, pass the db.getRoles() adocument with the following fields:

FieldTypeDescriptionrolesInfointegerSet this field to 1 to retrieve all user-defined roles.showPrivilegesbooleanOptional. Set the field to true to show role privileges, including both privilegesinherited from other roles and privileges defined directly. By default, thecommand returns only the roles from which this role inherits privileges anddoes not return specific privileges.showBuiltinRolesbooleanOptional. Set to true to display built-in roles as well as user-defined roles.

db.getRoles() wraps the rolesInfo command.

Required Access

To view a role’s information, you must be either explicitly granted therole or must have the viewRoleaction on the role’s database.

Example

The following operations return documents for all the roles on theproducts database, including role privileges and built-in roles:

  1. db.getRoles(
  2. {
  3. rolesInfo: 1,
  4. showPrivileges:true,
  5. showBuiltinRoles: true
  6. }
  7. )