MongoDB Permissions on Collection
You set up mongodb permissions on collection targets by using custom roles. Built-in database roles apply to every non-system collection, so collection limits require custom privileges. Teams use role-based rules to control who can read or write data. A role links a set of allowed actions to a specific database resource. While default roles cover whole databases, custom roles narrow access down to one collection. How mongodb permissions on collection targets work in practice MongoDB controls
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 permissions on collection targets by using custom roles. Built-in database roles apply to every non-system collection, so collection limits require custom privileges.
Teams use role-based rules to control who can read or write data. A role links a set of allowed actions to a specific database resource. While default roles cover whole databases, custom roles narrow access down to one collection.
How mongodb permissions on collection targets work in practice
MongoDB controls data access through roles. Each role grants permission to run specific actions on a defined resource. That resource can be an entire cluster, a single database, or one collection.
Standard built-in roles work at the database level. For example, the basic read role covers all non-system collections in a database. It allows actions like finding documents, listing collections, listing indexes, and checking stats. It blocks actions that change data, such as inserting, updating, removing, or dropping collections.
The read and write role includes every read action plus data change permissions. Users with this role can insert, update, and remove documents across the database. They can also create collections, build indexes, drop indexes, and drop collections. Self-hosted servers and cloud clusters offer different built-in roles, but both use these same actions.
When an engineer or AI agent needs access to one collection, built-in roles fall short. Database admins must build a custom role on that specific database instead. The custom role names the exact collection and lists the allowed actions. An admin might grant document find actions on an orders collection while blocking everything else. The user or agent then logs in and can only query that single collection.
Security and operational tradeoffs of collection roles
Custom collection roles reduce the blast radius of a stolen password or bad script. If an AI agent only has read rights on one collection, it cannot drop tables. It also cannot read secret collections stored in the same database.
Single Sign-On (SSO) + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Still, static roles and passwords leave major security gaps during an active session. Once a user has permission to read a collection, they see every field inside it. Collection rules do not hide sensitive fields inside the returned documents. A simple find query will still return plain emails, phone numbers, card numbers, and national IDs.
Managing dozens of custom roles across many databases also creates hard work for platform teams. Every new microservice, engineer, or AI tool needs its own role and password setup. Static keys sit in config files and chat logs long after a task ends. Role checks also lack an easy way to approve risky write commands one by one.
Why runtime controls matter for database sessions
Relying only on database roles forces teams to choose between speed and safety. If you lock down every collection, engineers wait hours for role changes during an outage. If you grant broad read and write roles, one mistake can wipe out production data.
Modern teams solve this by pairing basic database roles with an active runtime layer. Instead of handing out permanent database keys, the runtime layer checks identity on every query. It can ask a teammate to approve a write action before the database runs it. It can also apply data masking to rewrite emails or card numbers in query results. This keeps raw secrets safe while letting engineers and AI agents do their jobs fast.
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
Can built-in MongoDB roles limit access to a single collection?
No, standard built-in read and write roles apply to all non-system collections in a database. To limit a user to one collection, you must create a custom role.
What actions does the standard read role allow?
The read role allows finding data, listing collections, listing indexes, checking stats, and opening change streams. It does not allow inserting, updating, removing, or dropping data.
Do collection permissions hide sensitive document fields?
No, collection roles grant access to the entire document inside that collection. To rewrite sensitive fields like phone numbers or national IDs in results, you need data masking.
Related guides
In this post