How to Configure Codex MongoDB Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up codex mongodb read only access by creating a database user with the native read role. Then you pass that user login to the agent session. Many engineering teams start by sharing their own local database logins with AI coding agents. That habit is risky because developer accounts often hold broad write rights. If an agent misreads a prompt, a broad role lets it change or delete records. Scoping the database account down to read actions stops those accidental writes before they happen.

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 set up codex mongodb read only access by creating a database user with the native read role. Then you pass that user login to the agent session. Many engineering teams start by sharing their own local database logins with AI coding agents. That habit is risky because developer accounts often hold broad write rights. If an agent misreads a prompt, a broad role lets it change or delete records. Scoping the database account down to read actions stops those accidental writes before they happen.

How to set up codex mongodb read only access with native roles

MongoDB controls what a user can do through role-based rules. Each role grants a set of actions on a target resource. A role applies to the database where you define it. You can also scope custom roles down to the collection level. Note that MongoDB Atlas and self-hosted setups have different built-in role lists. Still, both setups build their roles from the same core privilege actions.

For a coding agent, the built-in read role is the standard starting point. This role lets the user read all non-system collections in that specific database. It permits query actions like finding documents, listing collections, listing indexes, and checking stats. It also allows opening a change stream on that database. Crucially, the read role leaves out actions that alter or remove data. It blocks inserts, updates, deletes, and dropping collections. By contrast, the readWrite role adds all of those data change actions back in.

Once you create the database user with the read role, you give those details to Codex. The agent uses that login string when it runs scripts or tools to inspect schemas and sample documents. Because the database enforces the role, the agent cannot run write commands on those collections.

What native read roles solve and what they leave open

Native database roles solve the problem of accidental data changes. When Codex logs in with a read role, it cannot drop a collection or overwrite records. This protects data integrity while the agent helps you write queries or debug issues. It is a solid first step for any team using AI coding tools.

Still, a database role only decides who can log in and which collection they can read. It does not inspect what individual queries do in flight. Even with a read-only role, the agent can read every field inside an allowed collection. If a collection holds emails, phone numbers, card numbers, or national IDs, the query returns them in plain text. Those raw values then enter the agent context and go to the model provider.

Auditor Read-Only Access + Customer Support Access to Production: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Native roles also require you to hand a static password or key to the agent workspace. If that login string leaks in a log file, anyone who finds it gets the same read access. Also, role checks cannot stop an agent from running a massive query that scans millions of documents. Database roles govern login rights at the collection level, not field privacy or live query behavior.

Why field privacy matters for AI coding agents

AI coding agents work best when they can see real document shapes and index stats. Giving them a read role on a staging or production database speeds up debugging. Yet real databases rarely keep sensitive fields in separate collections from safe fields. A single user document often mixes routine app settings with personal contact details.

Because MongoDB grants roles at the database or collection level, you face a hard choice. Either you block the whole user collection, or you expose every field inside it. When teams need the agent to check user queries, they usually allow the collection. Without runtime field masking that rewrites matched fields in results, sensitive strings flow straight into the prompt.

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

The read role lets a user find documents, list collections, check indexes, and view database stats. It does not allow inserts, updates, deletes, or dropping collections. The readWrite role includes everything in the read role plus actions that modify data and indexes.

Can I limit a MongoDB role to a single collection for Codex?

Yes, MongoDB lets you grant access down to the collection level of detail. While the built-in read role covers all non-system collections in a database, you can define custom roles. A custom role can restrict the find action to just the specific collections Codex needs.

Does a MongoDB read-only role hide sensitive fields from Codex?

No, a MongoDB role grants access at the database or collection level, not the field level. If Codex has the read role on a collection, its queries return entire documents. That includes emails, phone numbers, card numbers, or national IDs stored in those documents.

The query runs. The emails stay hidden.