How to Grant MongoDB Roles

In MongoDB, users are defined for specific databases. Each user is then assigned a list of roles that in turn define the user’s privileges.

Not surprisingly, MongoDB’s API therefore makes it easy to assign a user a list of roles via the grantRolesToUser method:

db.grantRolesToUser( "<username>", [ <roles> ], { <writeConcern> } )

However, MongoDB’s role API (http://docs.mongodb.org/v2.6/reference/method/js-role-management/) doesn’t directly cover another common use case where you want to assign a (maybe newly created) role or multiple roles to a list of users in one go.

In this tutorial, we’ll show you how to grant MongoDB roles to multiple users at once using Studio 3T’s Role Manager.

Let’s consider the following example. Say you have just created a new role “rwAdmin” on database “test” that makes users dbAdmins on that database and also let’s them read from and write to it. You now want to assign this new role and the existing role “userAdmin” to a group of users.

    1. Connect to your MongoDB server as a user that has sufficient privileges to grant roles to users, select the database where your users are defined, and click the “Users” icon in the toolbar to open the user management tab for that database.Grant roles to users by clicking the 'Users' button in the Global Toolbar
    2. You will now see a list of all your users in the selected database. Simply select all users that you want to grant those roles to and click the “Grant Roles…” button.Select the users you want to give roles to
    3. Now, select from the list of available roles those that you want to grant to your list of selected users, and click “Grant”.
      Choose from role types
    4. Et voilà 🙂  You can verify that those two roles were indeed granted to the group of selected users.Varify the roles given to the users

Once you’re done granting roles, read more about how to set up user authentication, create a MongoDB user, among and see what we’ve been up to lately.

Updated on September 27, 2018

Was this article helpful?

Related Articles