MongoDB Grant Role
A mongodb grant role gives a user or app specific rights on database resources. You use a mongodb grant role to control who can read or write data. Database teams rely on roles to protect live data from bad changes. Roles define clear rules for people, scripts, and AI agents. They group actions together and apply them to a database or collection. This model keeps daily permissions tidy and easy to check. How a mongodb grant role works in practice MongoDB uses roles to control all database ac
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.
A mongodb grant role gives a user or app specific rights on database resources. You use a mongodb grant role to control who can read or write data.
Database teams rely on roles to protect live data from bad changes. Roles define clear rules for people, scripts, and AI agents. They group actions together and apply them to a database or collection. This model keeps daily permissions tidy and easy to check.
How a mongodb grant role works in practice
MongoDB uses roles to control all database actions. A role pairs specific actions with target resources. Each role works inside the database where you define it. You can limit a role down to a single collection. Self-hosted setups and MongoDB Atlas use the same core privilege actions. Yet the two platforms offer different built-in role presets to users.
Most teams start with the two basic data roles. The read role lets a user read all non-system collections. It also allows read access on the system JavaScript collection. This role includes actions to find documents and list collections or indexes. It also allows users to check database stats, collection stats, and change streams. The read role blocks users from adding, changing, or deleting data. It also stops users from dropping a collection.
The read and write role includes every action from the read role. It adds the power to change data inside that database. Users with this role can insert, update, and remove documents. They can also create or drop collections and indexes. Engineers and AI agents often receive the read role to check live issues. When an engineer needs to fix data, a team admin grants the write role.
Security tradeoffs of static database roles
Static database roles work well for basic access control. They stop a read-only user from dropping a collection by mistake. Yet native roles have a hard limit at the collection boundary. Once a user has the read role, they can read every document inside that collection. They also read every field inside those documents.
Role-Based Access Control (RBAC) + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
This creates a big risk during an active session. A collection often holds sensitive fields like emails, phone numbers, or card numbers. A read role cannot hide these fields from a query result. If an engineer or AI agent runs a find query, the database returns raw data. Static keys and permanent roles make this problem worse over time.
Teams often share database keys to debug an urgent bug. Once a person or script gets a role, that permission often stays active forever. You cannot approve a single command before it runs with native roles alone. You also need a way to mask sensitive fields in flight. Data masking rewrites matched fields like national IDs in query results. Combining role rules with query checks and field masking closes these session gaps.
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
What is the lowest level a MongoDB role can control?
A role applies to the database where you define it. It can grant access down to the collection level of detail. Native roles do not restrict access to single documents or specific fields.
What is the difference between read and readWrite roles?
The read role lets users find data, list indexes, and view stats on non-system collections. The readWrite role adds actions to insert, update, and remove data, or drop collections and indexes.
Do MongoDB Atlas and self-hosted databases 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 set of underlying privilege actions.
Related guides
In this post