Grant MongoDB
To grant mongodb permissions, admins assign a role to a user on a target database. Each role pairs a set of allowed actions with specific database resources. Teams use this model to control what engineers, apps, and AI agents can do. A role works on the database where you define it. It can control access down to a single collection. How to grant mongodb roles in practice MongoDB uses role-based rules to control database actions. Every role joins a resource with a list of allowed actions. Bot
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 grant mongodb permissions, admins assign a role to a user on a target database. Each role pairs a set of allowed actions with specific database resources.
Teams use this model to control what engineers, apps, and AI agents can do. A role works on the database where you define it. It can control access down to a single collection.
How to grant mongodb roles in practice
MongoDB uses role-based rules to control database actions. Every role joins a resource with a list of allowed actions. Both self-hosted setups and cloud clusters build roles from the same core actions. Still, cloud database users have different built-in role options than self-hosted users.
Most teams start with two basic built-in roles. The read role lets a user read all non-system collections in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks write actions like insert, update, remove, and dropCollection.
When an engineer or app needs to change data, admins assign the readWrite role. This role includes everything in the read role plus data edit rights. It adds insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection.
AI agents and support engineers often need read access to check production bugs. Assigning the read role stops an agent or person from deleting records by mistake. If a user only needs one collection, a custom role limits actions to that collection.
What static roles cover and what stays exposed
Built-in roles solve part of the database security problem. They stop unauthorized writes and limit which collections a user can query. Yet static database roles leave big gaps during an active session.
First, collection roles work on the whole collection at once. If you allow the find action, the user can read every field in every document. Standard roles do not hide sensitive fields inside a permitted collection. When a query runs, the database returns full emails, phone numbers, card numbers, and national IDs. Real-time data masking fixes this by rewriting matched fields in query results.
MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Second, static roles often stay attached to long-lived database keys. Teams share these keys across laptops, scripts, and AI tools. When everyone uses the same database user, audit logs lose track of who ran which query. Tying database sessions to individual work identities solves this tracking problem.
Third, broad roles like readWrite allow dangerous actions such as dropCollection. An engineer fixing one bad record might run a bad command and wipe a collection. Adding command review steps lets teams check risky write actions before they run.
How to design safer database permissions
Start by separating human users from automated app accounts. Apps need stable permissions to run the same queries every day. Engineers and AI agents need scoped permissions for specific tasks.
Give human users and AI tools read-only roles by default. Only grant write actions when a specific fix requires data changes. Remove those write rights as soon as the task ends.
Scope custom roles to the exact collections a team needs to do its job. Avoid giving broad cluster permissions when a single database role works. Pair collection rules with field masking so queries rewrite card numbers and emails in results.
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 finest level of access a MongoDB role can control?
A role applies to the database where it is defined and controls access down to a single collection. It pairs that collection resource with specific allowed actions like find or insert.
What is the difference between the read and readWrite roles?
The read role allows read actions like find and listCollections on non-system collections. The readWrite role adds write actions like insert, update, remove, and dropCollection.
Do MongoDB roles hide sensitive fields inside a collection?
No, standard roles grant actions at the database or collection level. To protect emails, phone numbers, or card numbers in query results, teams add data masking to rewrite matched fields.
Related guides
In this post