MongoDB Roles and Privileges

Published
Length
3 min read

Luis Felipe Dias

All posts

In MongoDB, mongodb roles and privileges control which actions a user can run on a database. A role pairs allowed actions with specific database resources down to the collection level. Teams use these rules to control what people, apps, and AI agents can do. Good role design stops bad writes and keeps your data safe during daily work. How mongodb roles and privileges work in practice MongoDB uses role-based rules to grant access to data. Each role gives a user permission to run a set of acti

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.

In MongoDB, mongodb roles and privileges control which actions a user can run on a database. A role pairs allowed actions with specific database resources down to the collection level.

Teams use these rules to control what people, apps, and AI agents can do. Good role design stops bad writes and keeps your data safe during daily work.

How mongodb roles and privileges work in practice

MongoDB uses role-based rules to grant access to data. Each role gives a user permission to run a set of actions on a target resource. A role belongs to the database where you create it. It can grant access down to a single collection.

MongoDB Atlas users have different built-in roles than self-hosted database users. Both setups build their roles from the same basic privilege actions. Most teams start with two common built-in roles for daily work.

The read role lets users read all non-system collections in that database. It also covers the system JavaScript collection. This role allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not allow insert, update, remove, or dropCollection actions.

The readWrite role includes every action in the read role. It also adds the power to change data in the database. Its allowed actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Engineers and AI agents often use the read role to check data without breaking tables.

Security tradeoffs of static roles and active sessions

Static database roles are a strong first step for database safety. They stop a read-only user from dropping a collection or changing records. Still, native roles have clear limits during an active session.

MongoDB roles work down to the collection level, not the field level. If an engineer or AI agent has the read role, they can run find on the collection. That query returns every field in the matching documents.

Standing Privileges Elimination + MongoDB Authentication & Authorization: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

This means a read-only user still pulls raw emails, phone numbers, card numbers, and national IDs. Static credentials also stay valid until someone rotates or deletes them. If a key leaks, an attacker gets full collection access right away.

Shared database accounts make audits hard for security teams. When five engineers share one readWrite user, the database log shows one account name. You cannot tell which person ran a bad update or delete action.

Controlling queries and sensitive fields in live sessions

Modern teams add a control layer in front of their database roles. Instead of handing out static database keys, they tie database access to team logins. Engineers log in with their normal work account to start a short session.

This setup checks each command before it hits the database. A team can allow read queries while holding back risky write commands for peer approval. Every query links back to a real person or specific AI agent.

Data masking also fills the gap left by collection-level roles. When a query runs, masking rewrites matched fields in results before they reach the screen. Sensitive values like emails, card numbers, phone numbers, and national IDs turn into masked strings.

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

What is the finest level of access a MongoDB role can grant?

A MongoDB role applies to its home database and grants access down to the collection level. It does not restrict access to single documents or specific fields on its own.

What is the difference between the read and readWrite roles?

The read role allows actions like find, listCollections, and changeStream on non-system collections. The readWrite role adds write actions like insert, update, remove, createIndex, and dropCollection.

Do MongoDB Atlas and self-hosted MongoDB use the exact same built-in roles?

No, MongoDB Atlas database users have different built-in roles than self-hosted database users. Still, both platforms build their roles from the exact same set of privilege actions.

The query runs. The emails stay hidden.