## Prerequisites 

You must have the grantRole (page 420) action (page 419) on a database to grant a role on that database. 

To view a role’s information, you must be explicitly granted the role or must have the viewRole (page 420) action (page 419) on the role’s database. 

## Procedure 

Step 1: Connect with the privilege to grant roles. Connect to the mongod or mongos as a user with the privileges specified in the Prerequisites (page 391) section. 

The following procedure uses the siteUserAdmin created in Create a User Administrator (page 384). 

mongo --port 27017 -u siteUserAdmin -p password --authenticationDatabase admin 

Step 2: Identify the user’s roles and privileges. To display the roles and privileges of the user to be modified, use the db.getUser() and db.getRole() methods. 

For example, to view roles for reportsUser created in Add a User to a Database (page 386), issue: 

use reporting db.getUser("reportsUser") 

To display the privileges granted to the user by the readWrite role on the "accounts" database, issue: 

use accounts db.getRole( "readWrite", { showPrivileges: true } ) 

Step 3: Identify the privileges to grant or revoke. If the user requires additional privileges, grant to the user the role, or roles, with the required set of privileges. If such a role does not exist, create a new role (page 388) with the appropriate set of privileges. 

Step 4: Grant a role to a user. Grant the user the role using the db.grantRolesToUser() method. 

For example, the following grants new roles to the user reportsUser created in Add a User to a Database (page 386). 

use reporting db.grantRolesToUser( "reportsUser", [ { role: "readWrite", db: "products" } , { role: "readAnyDatabase", db:"admin" } ] ) 

## Verify User Privileges 

## Overview 

A user’s privileges determine the access the user has to MongoDB resources (page 417) and the actions (page 419) that user can perform. Users receive privileges through role assignments. A user can have multiple roles, and each role can have multiple privileges. 

