MongoDB Role Based Access Control
You use mongodb role based access control to restrict database actions per user. A role grants privileges to run sets of actions on defined database resources. Admins assign built-in or custom roles to engineers, apps, and AI agents. This model stops unauthorized writes or drops before a query runs. How mongodb role based access control works in practice In MongoDB, every role applies to the database where an admin defines it. A role can grant access down to a collection level of granularity.
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 use mongodb role based access control to restrict database actions per user. A role grants privileges to run sets of actions on defined database resources. Admins assign built-in or custom roles to engineers, apps, and AI agents. This model stops unauthorized writes or drops before a query runs.
How mongodb role based access control works in practice
In MongoDB, every role applies to the database where an admin defines it. A role can grant access down to a collection level of granularity. Self-hosted deployments and MongoDB Atlas use different sets of built-in roles. Still, both platforms build their roles from the same set of privilege actions.
Most teams start with the standard read role for daily debugging tasks. The read role gives access to all non-system collections in its database. It also allows reading the system.js collection within that same database. It permits actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks write actions like insert, update, remove, and dropCollection.
When an engineer or app must change data, teams assign the readWrite role. The readWrite role includes every action in the read role plus data modification. Its permitted actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Admins can also build custom roles that limit these actions to one collection.
AI agents and automated scripts also use these same database roles. An analytics agent uses a read role to query collections and inspect stats. A deployment script uses a readWrite role to add indexes and update documents. Database admins map each service account to the smallest role needed.
Security and operational tradeoffs of static database roles
Native roles handle coarse boundaries well, such as separating readers from writers. Yet roles stop at the collection level and do not filter fields inside documents. If a user can read a collection, they receive the whole document. That document often contains emails, phone numbers, card numbers, or national IDs.
Static roles also leave teams with long-lived database credentials to manage. Engineers often share a single readWrite account to fix a broken production service. Once a session starts with that role, every allowed action runs right away. A careless query can still drop a collection or delete thousands of records.
Role-Based Access Control (RBAC) + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Managing hundreds of custom roles across many clusters creates heavy work for admins. Each new developer or AI tool needs role updates across staging and production. Teams need a way to inspect queries and mask sensitive fields in flight. Data masking rewrites matched fields like emails or card numbers in query results.
Adding query controls and field masking to database sessions
Modern teams pair native database roles with session-level controls and approval checks. Instead of handing out permanent readWrite keys, admins keep default access read-only. When an engineer needs to run an update, they ask for approval first. Peers review the exact command before the database executes it.
Field-level controls also close the gap left by collection-level permissions. As results return from a find action, a policy engine checks the payload. It rewrites sensitive fields like phone numbers and national IDs in the output. This approach keeps daily work safe while preserving strict database boundaries.
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 lowest level of granularity for a MongoDB role?
A MongoDB role can grant access down to the collection level of granularity. It does not restrict access to individual documents or specific fields by itself. Teams use field masking to rewrite emails or card numbers in results.
What is the difference between the read and readWrite roles?
The read role allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. The readWrite role adds modification actions like insert, update, remove, createIndex, and dropCollection. Neither role grants access outside the database where it is defined.
Do MongoDB Atlas and self-hosted MongoDB use the same roles?
MongoDB Atlas database users have different built-in roles than self-hosted deployment users. Still, both platforms build their roles from the exact same privilege actions. You can still restrict actions down to the collection level on both.
Related guides
In this post