MongoDB Role Permissions

Published
Length
3 min read

Luis Felipe Dias

All posts

MongoDB role permissions control what actions a user or service can run on specific databases. Each role pairs a set of allowed actions with target resources down to the collection level. Teams use these rules to separate read tasks from write tasks across data stores. Engineers and automated scripts log in with assigned roles to query or change records safely. When teams grow, managing these database rules gets harder. How mongodb role permissions work in practice MongoDB uses roles to gran

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.

MongoDB role permissions control what actions a user or service can run on specific databases. Each role pairs a set of allowed actions with target resources down to the collection level.

Teams use these rules to separate read tasks from write tasks across data stores. Engineers and automated scripts log in with assigned roles to query or change records safely. When teams grow, managing these database rules gets harder.

How mongodb role permissions work in practice

MongoDB uses roles to grant access to data. A role groups specific actions and applies them to a database or a collection. A role applies to the database where an admin defines it. Self-hosted setups and cloud clusters offer different built-in role menus. Yet both models build their rules from the same core privilege actions.

For daily read tasks, admins often assign the basic read role. This role lets users read all non-system collections in that database. It allows actions like finding records, listing collections, and checking index lists. It also lets users check database stats, view collection stats, and watch change streams. It blocks users from adding, changing, or deleting data. It also blocks them from dropping a collection.

When an engineer or app needs to change records, admins use the read-write role. This role includes every action from the read role plus data change rights. Users can insert, update, and remove documents in the database. They can also create collections, build indexes, drop indexes, and drop collections. AI agents and support scripts also use these same roles when they query production data.

Security and operational tradeoffs of static roles

Native database roles stop unauthorized write actions, but they leave major gaps during active sessions. The finest grain for a MongoDB role is the collection level. If a user can read a collection, they can read every field in every document. That collection often holds sensitive fields like emails, phone numbers, card numbers, or national IDs. A read role returns all of those raw values in the query output.

Role-Based Access Control (RBAC) + MongoDB Authentication & Authorization: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static database credentials also create risk for engineering teams. Developers often store shared login keys in local files to run quick checks. Once an engineer or AI agent starts a session, the database trusts every allowed action. Native roles cannot pause a dangerous drop command to ask a peer for approval. They also cannot rewrite sensitive fields in query results on the fly.

Closing the session gap for database teams

To fix these gaps, teams put a policy layer in front of the database. Instead of handing out static keys, teams verify user identity before every session. A modern proxy layer checks who the user is and inspects queries in real time. Data masking rewrites matched fields like emails or card numbers in the query results. Teams can also require peer reviews for write actions while keeping read tasks fast.

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 most granular level for mongodb role permissions?

MongoDB roles can grant access down to the collection level within a database. Native roles do not restrict access to single documents or specific fields on their own.

What is the difference between the read and readWrite roles?

The read role lets users find data, view stats, and list collections or indexes. The read-write role adds rights to insert, update, remove data, and drop collections or indexes.

Do cloud and self-hosted MongoDB deployments use the same roles?

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

The query runs. The emails stay hidden.