MongoDB Document Level Access Control

Published
Length
3 min read

Luis Felipe Dias

All posts

Native roles do not offer direct mongodb document level access control out of the box. Instead, built-in database roles grant actions down to the collection level of detail. Teams that need record-level rules must build extra filters into their data workflow. They often use filtered views, app logic, or a database proxy to limit queries. This approach keeps engineers and AI agents from reading or changing restricted records. How mongodb document level access control works in practice MongoDB

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.

Native roles do not offer direct mongodb document level access control out of the box. Instead, built-in database roles grant actions down to the collection level of detail.

Teams that need record-level rules must build extra filters into their data workflow. They often use filtered views, app logic, or a database proxy to limit queries. This approach keeps engineers and AI agents from reading or changing restricted records.

How mongodb document level access control works in practice

MongoDB manages permissions through role-based rules. A role gives a user the right to run specific actions on a resource. You set up a role on a database, and it stops at the collection level. Both self-hosted setups and Atlas use the same core privilege actions for their roles. Still, Atlas database users have a different set of built-in roles than self-hosted users.

When an engineer or AI agent logs in, their role defines what they can do. The standard read role lets users read all non-system collections in that database. It also covers the system JavaScript collection. This role permits actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It blocks write actions like insert, update, remove, and dropCollection.

If a user needs to change data, admins assign the readWrite role. This role includes every action in the read role plus data change rights. It allows insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Because these actions apply to whole collections, a read user can fetch every record inside. To get true mongodb document level access control, you must filter queries outside the role.

Security and operational tradeoffs of collection roles

Relying only on collection roles creates major gaps during an active session. Once an engineer or AI agent logs in with a static key, the database trusts them. A read role stops them from dropping a collection or deleting a record. Yet it still lets them run a find query across every record in that collection.

Many collections mix safe operational data with private customer records in the same space. One record might hold public catalog info while the next holds card numbers. Collection-level roles cannot tell these two records apart during a query. If an engineer queries the collection to fix a bug, the database returns everything.

MongoDB Authentication & Authorization + Board-Level Security Reporting: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Teams sometimes try to fix this by splitting sensitive records into separate collections. That change forces developers to rewrite app queries and manage complex schema layouts. Other teams create custom read-only views that filter out restricted records by a field tag. Views help with read limits, but admins must update them whenever data rules change. Also, static database keys still sit on user laptops or inside agent config files. If a key leaks, an attacker gains full collection rights until someone rotates the secret.

Protecting fields and queries during live database sessions

A better way to handle record limits combines identity checks with live query controls. Instead of sharing static database keys, engineers and AI agents log in with their work identity. The system checks who they are before they ever run a database command.

When a user needs to run a write action or read a restricted collection, they ask for approval. A peer can review and approve the request right in their chat app. For read queries, data masking rewrites matched fields in results before they reach the screen. Sensitive fields like emails, phone numbers, card numbers, and national IDs get masked on the fly. This setup gives teams tight control over records and fields while keeping database roles simple.

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

Does MongoDB support document level permissions in built-in roles?

No, built-in roles only grant access down to the collection level. To limit access to single records, you must use views, app filters, or an external query layer.

What is the difference between the read and readWrite roles?

The read role lets users run find and stats commands on non-system collections. The readWrite role adds actions to insert, update, remove, and manage indexes or collections.

How can teams protect sensitive fields inside a shared collection?

Teams can place a proxy layer in front of the database to apply data masking. Masking rewrites matched fields like emails, phone numbers, and card numbers in query results.

The query runs. The emails stay hidden.