How to Add Roles to Existing User in MongoDB

Tek Loon
Tek Loon
Jan 12 · 3 min read
Image for post
Image for post

Scenario

Imagine you’re a DevOps engineer and you are responsible to grant access to the database, which could be known as Database Administrator as well.

However, it’s a new year and your company management decided to promote some of the potential team members to become the team leaders. 🎉🎉🎉

As you all known:

With great responsibility comes great power. — Mark Manson

Let’s call them, “ devA” and “ devB”. In the next section, we would show the step-by-step guide on how to grant the read & write access to both users.

Step-by-Step Guide

Before we go into the detailed step on how to grant extra roles to the existing users. Here is the short and concise version of the steps.

  1. List all users in the admin database (If all your user is created inside admin database).
  2. Select the ‘user’ field with a matching values of “devA” and “devB”.
  3. Use grantRolesToUser() function to give devA and devB users with the built-in role - readWriteAnyDatabase.

Step 1. List all users in the admin database

Image for post
Image for post

Step 2. Find the ‘user’ field value

Look at the field name called user and found the value as we would need this in the next step. Refer to the example below.

Step 3. Use grantRolesToUser function to grant read & write roles

With the simple command below, you will be able to allow both users have read & write access to all the databases except local & config.

use admin;
db.grantRolesToUser('devA', ['readWriteAnyDatabase']);
db.grantRolesToUser('devB', ['readWriteAnyDatabase']);

However, if you would like to only give read & write access to a specific database. For e.g, you would like to give read & write access to the client database only. You can do it via the below code.

use admin;
db.grantRolesToUser('devB', [{ role: 'readWrite', db: 'client' }]);

Conclusion

In short, this post discussed:

  • How to add more roles to the existing users without creating a new one in MongoDB
  • How to add read & write access to all the databases to an existing user.
  • How to add read & write access to a single database only to an existing user.

Thank you for reading and see you in the next article.

References

  • MongoDB grantRolesToUser Manual
  • MongoDB Built-In Roles Manual

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store