MongoDB Collection Access Control
You set up mongodb collection access control by creating custom roles with collection-specific privileges. These roles bind specific actions to single collections rather than the whole database. Built-in database roles apply to every non-system collection in a database by default. Teams use custom privilege sets to restrict engineers and AI agents to approved collections. How mongodb collection access control works in practice MongoDB controls permissions through role-based rules. A role give
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 collection access control by creating custom roles with collection-specific privileges. These roles bind specific actions to single collections rather than the whole database. Built-in database roles apply to every non-system collection in a database by default. Teams use custom privilege sets to restrict engineers and AI agents to approved collections.
How mongodb collection access control works in practice
MongoDB controls permissions through role-based rules. A role gives a user permission to run specific actions on defined resources. Each role belongs to the database where an admin defines it. By default, built-in roles grant rights across an entire database.
For example, the built-in read role covers all non-system collections in a database. It also covers the system JavaScript collection. This role permits actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks write actions like insert, update, remove, and dropCollection.
The built-in readWrite role includes every permission from the read role. It also adds actions that change data and structure. These include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Because built-in roles apply to all collections, they are often too broad.
To narrow permissions, admins build custom roles. A custom role can grant access down to a single collection. MongoDB Atlas and self-hosted setups use different built-in role lists. Still, both platforms build permissions from the same set of privilege actions. When an engineer or AI agent logs in, the database checks their role. If the role lists the target collection and action, the query runs.
Security and operational tradeoffs of static roles
Collection-level roles stop users from reading or changing unapproved collections. Still, static roles leave major gaps during an active database session. First, teams must create and maintain many custom roles across multiple databases. As schemas grow, role sprawl becomes hard for platform teams to track.
Second, collection access is all-or-nothing for the documents inside that collection. If an engineer has read rights on a customer collection, they pull full documents. Those documents often hold sensitive fields like emails, phone numbers, card numbers, or national IDs. Native collection roles do not rewrite or mask individual fields in query results.
MongoDB Authentication & Authorization + Evidence Collection Automation: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Third, shared database keys create audit blind spots. Teams often share one readWrite key for support tasks or AI tools. When someone runs a bad update or remove action, logs only show the shared role. Admins cannot tie the command back to a specific person.
Finally, standing roles stay active all day. An engineer who needs write rights for ten minutes keeps those rights forever. If an attacker steals that static key, they get full collection access right away.
Closing session gaps with command checks and masking
Modern teams pair database roles with an external enforcement layer. Instead of handing out static database keys, users log in with their work identity. The enforcement layer checks every command before it reaches the database cluster.
This approach lets teams approve single write queries on the fly. An engineer can hold default read permissions and ask for temporary write rights when needed. Also, data masking policies inspect query results on the way back. Masking rewrites matched fields like emails, phone numbers, and card numbers in results. Engineers and AI agents can debug real collections while sensitive fields stay hidden.
Check MongoDB commands and documents on the wire
MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.
Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.
Frequently asked questions
Can MongoDB restrict access to a single collection?
Yes, MongoDB roles can grant access down to the collection level. Admins define custom roles that pair specific actions with one target collection. Built-in roles like read and readWrite apply to all non-system collections in a database.
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 includes all read actions plus data changes. It adds insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection.
Do MongoDB Atlas and self-hosted MongoDB use the same roles?
MongoDB Atlas database users have different built-in roles than self-hosted database users. Still, both platforms build their roles from the exact same set of privilege actions. You can set up collection-level rules in both environments.
Short role checks help teams work fast. Simple rules make each review clear. Good logs keep every team safe. Scoped keys stop bad changes early.
Related guides
In this post