How to Configure MongoDB Atlas Row Level Security

Published
Length
3 min read

Luis Felipe Dias

All posts

You cannot set up mongodb atlas row level security with native database roles alone. MongoDB roles only grant access down to the collection level of detail. Teams often work around this limit by building filtered views or app logic. If a team grants broad collection roles to engineers, those users can read every document. That includes private rows meant for other tenants or regions. How mongodb atlas row level security works in practice MongoDB uses role-based rules to control what a databas

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.

You cannot set up mongodb atlas row level security with native database roles alone. MongoDB roles only grant access down to the collection level of detail. Teams often work around this limit by building filtered views or app logic. If a team grants broad collection roles to engineers, those users can read every document. That includes private rows meant for other tenants or regions.

How mongodb atlas row level security works in practice

MongoDB uses role-based rules to control what a database user can do. A role grants a user the right to run actions on set resources. Each role applies to the database where an admin defines it. The finest level a native database role can reach is a single collection. MongoDB Atlas users have different built-in roles than self-hosted users, though both use the same privilege actions.

For basic access, admins often assign the built-in read role to users. This role lets a user read all non-system collections in that database. It allows actions like finding documents, listing collections, and checking collection stats. It does not allow users to insert, update, remove, or drop collections. When users need to change data, admins grant the readWrite role instead. That role adds actions to insert, update, remove, and drop collections or indexes.

Because native roles stop at the collection boundary, true document filtering requires extra steps. Teams usually split tenant data into separate collections or build read-only views. An admin can grant a user read access to a filtered view alone. That user can then query the view without reading the base collection directly.

What native collection roles solve and what they miss

Collection roles and views work well for stable, predictable app access patterns. They keep standard apps from modifying data when they only need read rights. Views also help when a small group of users needs a fixed filter. You can separate whole collections by team and assign scoped roles to each group.

Still, this setup breaks down during live debugging and support tasks. Views take time to build and maintain as schemas and teams grow. Engineers who fix production bugs often get broad read or readWrite roles on base collections. Native database roles only check who logs in and which collection they touch. They do not inspect what individual queries do in flight during a live session.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Once a user has read rights on a collection, every document is open. If documents contain emails, phone numbers, card numbers, or national IDs, queries return them raw. Field-level masking is also missing from basic collection roles. Real data protection requires a way to rewrite matched fields in query results on the fly. It also requires reviewing risky write commands before they run against a shared collection.

Managing document access across engineering teams

Growing teams need a clear plan to keep shared collections safe during daily work. First, remove default readWrite roles from human users who only run checks. Give each person or app a distinct role scoped to the collections they need. Avoid sharing one broad database login across multiple engineers or support staff.

Second, decide how to handle sensitive fields inside shared document collections. Instead of maintaining dozens of custom views, apply masking rules to sensitive fields. When a support engineer queries a user document, masking rewrites emails and card numbers in results. This lets the team fix app issues while keeping private tenant details safe.

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 have native row level security on collections?

No, native MongoDB roles only grant access down to the collection level. To restrict specific documents, you must use filtered views or external access controls.

What is the difference between the read and readWrite roles?

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

How can teams protect sensitive fields inside a shared collection?

Teams can use data masking to rewrite emails, phone numbers, and card numbers in results. This protects private fields even when a user has read access to the collection.

The query runs. The emails stay hidden.