How to Create Read-Only Role in MongoDB
This post discussed how to create a read-only role in the MongoDB database. Without further ado, let’s start.
Scenario
Imagine you’re a DevOps engineer or the team leader for the Backend team, normally these roles are the role to grant access to the database.
There are two newly hired developers who just joined your team and the management decided to provide a read-access only to the database in the staging environment in order to ease the debugging workflow. Let's call them Developer A and Developer B.
Here is the list of Mongo databases that is available.

Let’s summarized the scenario and break it down into two tutorials.
Method 1 — Create a user who has read-only access to the client database for Developer A.
Method 2 — Create a user which have read-only access to all databases for Developer B
Method 1
Firstly, we would have to create a user to access the DB. Although you could create a user in any database. But I recommend creating all users in admin database so it is easier to manage. So in the future, you hired a DB Administrator and you could just grant him access to the admin database so he could help you manage the DB user role creation without exposing any other database.
In this tutorial, we will be using Mongo Shell to create users.
use admin; // Make sure we go to the admin database // Create devA user and read access only to client DB. db.createUser({
user: "devA",
pwd: "devA",
roles: [ { role: "read", db: "client" } ]
});Using the simple above command we will now have a devA user who has only read-access to the client database and you can see the user successfully added in the below screenshot.

Let’s verify it using MongoDB Compass. The below screenshots showed that we’re trying to connect to the database using the newly-created user, “devA”.
- We can only see client database even though there are total of 5 databases.
- Trying to create a collection and get an “Unauthorized” error which is exactly what we want. Read-access only.



Method 2
This tutorial is way easier than you could imagine. There is a built-in role called readAnyDatabase where you can grant to the user
use admin; // Make sure we go to the admin database// Create devB user and provide read access all of the DB.
db.createUser({
user: "devB",
pwd: "devB",
roles: ["readAnyDatabase"]
});Now let’s verify the newly added user, devB has access to all the databases. From the screenshots below, we can see devB has read-only access to all the databases and creating new records is also prohibited.
MongoDB 3.4 onwards, readAnyDatabase no longer provides read access to local and config database.
Provides the same read-only privileges as read on all databases except local and config. The role also provides the listDatabases action on the cluster as a whole. — MongoDB Docs


Conclusion
In short, this post discussed two different scenarios on how to:
- Create read-only access to the specified database
- Create read-only access to all of the databases exclude local and config databases.
Thank you for reading and see you in the next article.
References
- MongoDB createUser Manual Reference
- MongoDB Manual for Built-in Roles






