MongoDB Grant Access to Collection
To make mongodb grant access to collection resources, you create a custom role. This role assigns specific actions to one collection inside a target database. Built-in roles like read and readWrite cover all non-system collections in a database. When a user or AI agent needs just one collection, you need custom rules. You set the target database, name the single collection, and list allowed actions. How mongodb grant access to collection rules work in practice MongoDB uses role-based rules to
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 make mongodb grant access to collection resources, you create a custom role. This role assigns specific actions to one collection inside a target database. Built-in roles like read and readWrite cover all non-system collections in a database. When a user or AI agent needs just one collection, you need custom rules. You set the target database, name the single collection, and list allowed actions.
How mongodb grant access to collection rules work in practice
MongoDB uses role-based rules to control what users and apps can do. A role links a set of allowed actions to a specific resource. Each role lives on the database where an admin defines it. From there, it can grant permissions down to a single collection level.
Standard built-in roles are often too wide for strict security needs. The built-in read role covers every non-system collection in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks write actions like insert, update, remove, and dropCollection.
The readWrite role goes further across the whole database. It includes all read actions and adds the power to change data. Users with readWrite can run insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Self-hosted setups and MongoDB Atlas have different built-in role lists. Still, both systems build their roles from the same basic privilege actions.
To lock down an AI agent or engineer, admins build a custom role. The admin names the exact database and the single collection inside the role. Then the admin picks only the needed actions, such as find. Finally, the admin assigns that custom role to the database user account.
Security tradeoffs of static collection roles
Collection roles stop users from touching other tables in the same database. This works well for service accounts that run a single app task. Yet static roles have limits when real people or AI agents run queries.
First, collection roles cannot filter which fields come back in a query. If a user can run find on a customer collection, every document field returns. That collection might hold emails, phone numbers, card numbers, or national IDs. A native collection role sends all those raw fields back in the results. Data masking is needed to rewrite matched fields in results during live queries.
Customer Support Access to Production + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Second, managing dozens of custom roles across many clusters takes significant work. Every time an engineer debugs a bug, an admin must update user roles. Often, admins grant full database read roles just to save time. Also, shared database passwords stay valid long after the fix is done.
Managing live queries and collection permissions
Teams get better control when they pair database roles with live query rules. Instead of giving every user a permanent database account, teams check identity per session. An engineer logs in with a work identity to run a quick check. A policy layer checks if that person can query the target collection right now.
This setup also helps control write actions in production. Rather than leaving readWrite active all day, a team can require peer approval. When an engineer sends an update command, a teammate reviews the request first. At the same time, data masking rewrites emails and card numbers in query results. Every query and result check goes into a central audit log for review.
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 a user to one collection?
No, built-in roles like read and readWrite apply to the whole database. They grant permissions across all non-system collections in that database. To limit a user to one collection, you must create a custom role.
What actions are included in the built-in read role?
The built-in read role lets users read all non-system collections and system.js. It includes actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not allow insert, update, remove, or dropCollection.
Do collection roles hide sensitive fields inside documents?
No, a collection role controls which actions a user can run on the collection. If a role allows the find action, queries return the whole document. Teams use data masking to rewrite emails, phone numbers, or card numbers in results.
Related guides
In this post