MongoDB Create Read Only User

Published
Length
3 min read

Luis Felipe Dias

All posts

To handle a mongodb create read only user task, you assign the built-in read role. This role lets users query data on a target database without making changes. Teams often need this setup when developers or AI agents inspect live data. A read-only account prevents accidental deletes or bad updates during a debug session. It separates safe data viewing from risky data writing across your databases. How a mongodb create read only user setup works in practice MongoDB controls database access th

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.

To handle a mongodb create read only user task, you assign the built-in read role. This role lets users query data on a target database without making changes.

Teams often need this setup when developers or AI agents inspect live data. A read-only account prevents accidental deletes or bad updates during a debug session. It separates safe data viewing from risky data writing across your databases.

How a mongodb create read only user setup works in practice

MongoDB controls database access through role-based rules. Each role pairs a set of allowed actions with specific database resources. When you set up a user, you bind their account to a specific database.

The built-in read role grants access to all non-system collections in that database. It also covers the system JavaScript collection. This role permits actions like finding documents, listing collections, and checking index lists. Users can also view database stats, collection stats, and open change streams.

Still, the read role blocks any action that alters state. The user cannot insert new documents, update existing records, or remove data. It also blocks dropping collections or changing indexes.

In contrast, the read and write role includes all read actions plus modification rights. That broader role allows inserts, updates, removes, and collection drops. By sticking to the read role, you keep the account safe for basic checks.

Sometimes a user only needs to see one collection inside a database. MongoDB roles can grant access down to a single collection level. You can build a custom role that only allows finding data in one collection. Note that MongoDB Atlas users have different built-in roles than self-hosted users. Still, both setups rely on the exact same underlying privilege actions.

Security and operational tradeoffs of static read roles

Assigning a read role stops data edits, but it does not solve every risk. A read-only user can still pull every document inside an allowed collection. If that collection holds customer records, the user sees all of them.

Read-Only Root Filesystem + User Provisioning (SCIM): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Sensitive fields like emails, phone numbers, card numbers, and national IDs stay visible. A developer or AI tool running a find query receives the raw values. Data masking is needed if you want to rewrite matched fields in query results.

Static database accounts also create upkeep work for your engineering team. When you make a database user, you usually hand out a static password. That password often ends up in local files or shared team chats. If an engineer leaves the team, you must rotate that shared key right away.

Also, database roles apply to the database account, not the human using it. If five people share one read-only user, the database logs show one name. You cannot tell which person ran a heavy query that slowed down the cluster. You also lack an approval step before someone runs a wide read query.

Scoping read permissions for engineers and AI agents

Modern teams give database read rights to both human engineers and automated AI agents. An AI agent might query collections to summarize support tickets or check order status. If the agent uses a broad role, it can read every collection in the database.

To limit this risk, scope the role to only the collections the agent needs. Combine collection-level roles with field-level masking for emails, phone numbers, and card numbers. This step rewrites sensitive values in the results before they reach the agent.

Finally, review who holds read roles on a regular schedule. Temporary debug needs should not turn into permanent standing permissions on production data.

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 difference between the read role and readWrite role in MongoDB?

The read role allows viewing data, listing collections, and checking stats without making changes. The read and write role adds actions to insert, update, remove, and drop collections.

Can a MongoDB role restrict read access to a single collection?

Yes, MongoDB roles can grant access down to the collection level of detail. You can define a role that allows read actions on one specific collection only.

Does a read-only user hide sensitive fields like emails or card numbers?

No, a standard read role returns the full document from any allowed collection. You need data masking in the query path to rewrite emails or card numbers.

The query runs. The emails stay hidden.