MongoDB Access Control
MongoDB access control uses roles to decide which database actions a user can run. Each role sets rules down to the collection level for your team. Good database security needs clear rules for both people and AI tools. When an engineer logs in, the database checks their assigned role first. This check happens before any read or write command can run. Setting up these roles the right way stops bad commands from breaking data. How mongodb access control works in practice MongoDB uses role-base
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.
MongoDB access control uses roles to decide which database actions a user can run. Each role sets rules down to the collection level for your team.
Good database security needs clear rules for both people and AI tools. When an engineer logs in, the database checks their assigned role first. This check happens before any read or write command can run. Setting up these roles the right way stops bad commands from breaking data.
How mongodb access control works in practice
MongoDB uses role-based rules to grant actions on specific database resources. A role applies to the database where you define it. It can grant permissions down to a single collection. Cloud setups in Atlas and self-hosted setups use different built-in role lists. Both types of setups build their roles from the same privilege actions.
Two common built-in roles are the read role and the readWrite role. The read role lets a user read all non-system collections in a database. It also covers the system JavaScript collection. It allows actions like find, list collections, list indexes, and watch change streams. It also lets users check database stats and collection stats. The read role blocks insert, update, remove, and drop collection actions.
The readWrite role includes every permission from the read role. It also adds the power to change data in that database. Its actions include insert, update, remove, and create collection. It also lets users create indexes, drop indexes, and drop collections. Engineers and AI agents need one of these roles to run queries.
Tradeoffs of static keys and collection roles
Built-in roles do a great job of stopping unwanted writes. You can give an AI agent or analyst the read role. That user cannot drop a collection or delete a document by mistake. Still, collection-level roles have clear limits during an active session.
First, teams often share static database keys across many apps and people. When five engineers share one login key, the database logs show one user. You cannot tell which person ran a heavy query during an outage. Rotating that shared key also breaks scripts and takes hours of team time.
MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Second, collection-level roles are all-or-nothing for the documents inside that collection. If a role allows find on a customer collection, it returns whole documents. Those documents often hold sensitive fields like emails, phone numbers, or card numbers. They might also hold national IDs alongside safe app data. Native roles cannot rewrite matched fields in query results on the fly.
Adding query review and field masking for teams
Modern teams pair native database roles with a central broker layer. Instead of handing out raw database keys, engineers log in with their work identity. The broker maps that person to the right database role for that session. Every query links back to a real person or a specific AI agent.
This setup also fixes the problem of sensitive fields inside large collections. As query results return, data masking rewrites matched fields in the results. It can replace emails, phone numbers, card numbers, and national IDs with safe tokens. Engineers still debug app issues with real document shapes. For rare write tasks, a teammate can approve a single command before it runs.
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
How fine-grained are native MongoDB roles?
Native roles apply to a database and work down to the collection level. They control which actions a user can run on those collections. They do not filter or mask individual fields inside a returned document.
What is the difference between the read and readWrite roles?
The read role lets users run find, list collections, and check stats. It blocks all data changes. The readWrite role adds actions like insert, update, remove, and drop collection.
How can teams protect emails and card numbers during read queries?
Teams can place a proxy layer in front of the database. This layer inspects query results and rewrites matched fields like emails or card numbers. The database keeps its normal roles while results get masked in flight.
Related guides
In this post