How to Configure MongoDB User Roles
You set up mongodb user roles by linking allowed actions to target databases or collections. You then assign those roles to user accounts so people only run approved commands. Many teams start by giving developers broad database rights to move fast during early builds. When roles stay too wide in production, one bad query can expose or wipe data. How mongodb user roles work at the database level MongoDB uses role-based rules to control what a user can do after they log in. Each role pairs a r
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You set up mongodb user roles by linking allowed actions to target databases or collections. You then assign those roles to user accounts so people only run approved commands. Many teams start by giving developers broad database rights to move fast during early builds. When roles stay too wide in production, one bad query can expose or wipe data.
How mongodb user roles work at the database level
MongoDB uses role-based rules to control what a user can do after they log in. Each role pairs a resource, like a database or collection, with a list of actions. A role applies to the database where you define it. It can grant rights down to the single collection level. Self-hosted setups and MongoDB Atlas have different built-in role lists. Both systems still use the same base privilege actions under the hood.
Most teams start with two built-in options for daily work. The read role lets a user read all non-system collections in that database. It allows actions like find, list collections, list indexes, and check collection stats. It also allows change streams and reading the system JavaScript collection. It blocks data changes such as insert, update, remove, or drop collection.
The read and write role includes every action from the read role. It also adds the power to change data inside that database. Users with this role can insert, update, and remove documents. They can also create or drop collections and indexes when needed. When built-in options feel too wide, admins build custom roles for specific collections.
Why you should separate admin duties from data queries
Teams often make the mistake of mixing admin rights with daily data work. If every engineer gets full write rights, one bad command can drop a collection. A safer plan splits daily read tasks from schema changes and user management. You can give most team members the basic read role on specific databases.
You then keep write roles for apps and a few trusted leads. When a developer needs to check slow queries, they only need index and stats actions. They do not need the power to remove documents or drop collections. Scoping roles down to the collection level keeps mistakes small and easy to fix. It also stops an app bug in one service from touching other collections.
User Provisioning (SCIM) + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What native roles solve and the gap they leave open
Native roles are great at deciding who can log in and touch a collection. They enforce hard walls between databases and stop unauthorized users from changing data. They also let you separate read users from write users with clear rules.
Still, a database role stops at the collection level once a session starts. If a user has read rights on a customer collection, they get whole documents back. Native roles do not rewrite fields like emails, phone numbers, or card numbers in results. They also cannot require a peer to approve a single risky delete command in flight. Finally, sharing static database passwords across a team makes it hard to track individual queries.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.
Frequently asked questions
What is the finest level of control for a role in MongoDB?
A role can grant access down to the collection level within its defined database. Native roles do not restrict access to specific fields or single documents on their own.
What is the difference between the read and readWrite roles?
The read role allows finding data, listing collections or indexes, and viewing stats. The readWrite role adds actions to insert, update, remove data, and create or drop collections.
Do MongoDB Atlas and self-hosted setups use the same roles?
MongoDB Atlas database users have a different set of built-in roles than self-hosted users. Still, both systems build their roles from the exact same set of underlying privilege actions.
Related guides
In this post