MongoDB Grant Access to Database
To set up a mongodb grant access to database rule, you assign a role to a user. That role controls which actions the user can run on collections inside that database. Every role pairs a list of allowed actions with a target resource. A role works on the database where you define it. It can also limit permissions down to a single collection. Self-hosted servers and cloud clusters offer different built-in roles. Still, both platforms build their roles from the same core privilege actions. How a
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.
To set up a mongodb grant access to database rule, you assign a role to a user. That role controls which actions the user can run on collections inside that database.
Every role pairs a list of allowed actions with a target resource. A role works on the database where you define it. It can also limit permissions down to a single collection. Self-hosted servers and cloud clusters offer different built-in roles. Still, both platforms build their roles from the same core privilege actions.
How a mongodb grant access to database role works
Engineers and AI agents need clear permissions before they query a cluster. You do not give out raw admin keys for daily work. Instead, you pick a built-in role or build a custom one.
The basic read role lets a user read all non-system collections in one database. It also covers the system JavaScript collection. This role allows actions like find, list collections, list indexes, and watch change streams. It also lets users check database stats and collection stats. It blocks write actions like insert, update, remove, and drop collection.
When a developer or an app needs to change data, you assign the readWrite role. This role includes every permission from the read role. It also adds actions to insert, update, and remove documents. Users with this role can create or drop collections and indexes too.
Sometimes an admin or an automated tool must check several databases at once. For those cases, you can assign cluster-wide read roles from the admin database. You can also create custom roles when built-in options are too broad. A custom role lets you grant find actions on one collection while blocking others.
What static roles cover and what stays exposed
Native database roles do a good job of stopping unauthorized writes or drops. They draw a clear boundary around which collections a user or agent can touch. If a support engineer only has the read role, they cannot delete a table.
Database Access Proxy + Customer Support Access to Production: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Still, role-based rules have limits during an active session. Once a role grants read permissions on a collection, the user gets every field. MongoDB roles stop at the collection level. They do not filter out sensitive fields inside a document by default.
A customer collection often holds emails, phone numbers, card numbers, and national IDs. An engineer or AI agent with the read role will pull all of those fields. Static user keys also stay active until someone rotates or deletes them. If a shared key leaks, anyone with that key keeps full role permissions.
Teams also struggle to review what happened inside a shared role session. Five engineers might log in with the same readWrite account to fix a bug. Native role checks cannot tell which person ran a bad update command. Adding approval steps before someone runs a destructive action is also hard with roles alone. To close these gaps, teams pair native roles with query-level controls and data masking. Data masking rewrites matched fields like emails or card numbers in query results.
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 safest role for an engineer who only needs to debug data?
Assign the built-in read role on the specific target database. It allows find and stats actions on non-system collections while blocking all data changes.
Can a native MongoDB role limit permissions to a single collection?
Yes, MongoDB roles can grant actions down to the collection level. You can build a custom role that allows find on one collection only.
What is the difference between the read and readWrite roles?
The read role only lets users view documents, indexes, and stats. The readWrite role adds permissions to insert, update, remove, and drop collections or indexes.
Related guides
In this post