MongoDB Read Only Role
A mongodb read only role lets users view data without changing or deleting it. MongoDB uses the built-in read role to grant read access on a database. Teams assign this role to developers, analysts, and AI agents for safe data checks. It stops accidental writes and drops on production collections. Yet it still returns every field inside each allowed collection. How the mongodb read only role works in practice MongoDB controls access through role-based authorization. Each role gives permission
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 read only role lets users view data without changing or deleting it. MongoDB uses the built-in read role to grant read access on a database. Teams assign this role to developers, analysts, and AI agents for safe data checks. It stops accidental writes and drops on production collections. Yet it still returns every field inside each allowed collection.
How the mongodb read only role works in practice
MongoDB controls access through role-based authorization. Each role gives permission to run specific actions on set resources. A role applies to the database where you define it. You can scope permissions down to a single collection level. Self-hosted servers and MongoDB Atlas have different built-in roles. Still, both platforms build their roles from the same core privilege actions.
The built-in read role covers all non-system collections in its target database. It also includes access to the system JavaScript collection. This role permits actions like find, listCollections, and listIndexes. Users can also check database stats, collection stats, and change streams. The role blocks actions that alter data, such as insert, update, and remove. It also blocks the dropCollection action so users cannot delete whole tables.
Engineers and AI agents often use this role during daily support work. An agent can run a find action to debug an app error. A developer can check indexes and stats to fix a slow query. Neither the human nor the bot can break production records by mistake. If someone needs to change records, they need the readWrite role instead. That wider role adds insert, update, remove, index changes, and collection drops.
Security tradeoffs of static roles and credentials
Assigning a read role is a strong first step for database safety. It prevents destructive commands from ruining your stored data. Still, collection-level read permissions leave major gaps during an active session. When a user can read a collection, they can read every document inside. They also get every field inside those documents.
Read-Only Root Filesystem + Role-Based Access Control (RBAC): architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Production collections often hold private user details alongside normal app data. A simple read query will return emails, phone numbers, card numbers, and national IDs. Static database roles cannot hide specific fields inside a returned document. If an AI agent or contractor runs a broad query, private data flows out. Data masking is needed to rewrite matched fields in results before they arrive.
Static database keys create another operational burden for engineering teams. Teams often share one read-only user key across many developers or scripts. When that key runs a heavy query, audit logs only show the shared role. You cannot tell which team member ran the query. Rotating those shared keys also takes manual work across many apps and tools. Tying database sessions to individual user logins fixes this tracking gap.
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 difference between the read and readWrite roles in MongoDB?
The read role lets users view non-system collections, indexes, stats, and change streams. The readWrite role includes all read permissions plus actions that change data. Those extra actions include insert, update, remove, index creation, and dropping collections.
Can a MongoDB role restrict access to a single collection?
Yes, MongoDB roles apply to the database where you create them. You can grant access down to the collection level of granularity. This lets you limit a user to reading only one specific collection.
Does the read role hide sensitive fields like emails or card numbers?
No, the built-in read role grants access to whole collections, not safe fields. Any find action returns the full document, including emails, phone numbers, or card numbers. You need field-level data masking to rewrite sensitive values in query results.
Related guides
In this post