How to Configure LlamaIndex MongoDB Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up llamaindex mongodb read only access by giving your app a restricted database role. This role lets the AI agent read documents without changing or deleting your data. Many teams start by reusing an admin key or a read-write user during testing. When that wide role reaches production, a bad prompt or agent bug can change records. Giving the agent a read-only user is the first step to safe database queries. How to set up llamaindex mongodb read only access with roles MongoDB controls

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 llamaindex mongodb read only access by giving your app a restricted database role. This role lets the AI agent read documents without changing or deleting your data. Many teams start by reusing an admin key or a read-write user during testing. When that wide role reaches production, a bad prompt or agent bug can change records. Giving the agent a read-only user is the first step to safe database queries.

How to set up llamaindex mongodb read only access with roles

MongoDB controls who can do what through role-based rules. A role gives a user the right to run specific actions on a resource. Each role applies to the database where you define it. You can also set rules down to a single collection if you build custom roles. Notice that MongoDB Atlas and self-hosted setups have different built-in role lists. Both systems still build their roles from the exact same set of basic actions.

For an AI agent, you should assign the built-in read role on the target database. This read role lets the user read all non-system collections in that database. It allows safe actions like finding documents, listing collections, listing indexes, and checking database stats. It also allows opening a change stream. It leaves out write actions like inserting, updating, removing data, or dropping a collection.

By contrast, the readWrite role includes everything in the read role plus data changes. It lets a user insert, update, or remove documents, and create or drop indexes. Official LlamaIndex guides warn that running generated queries from an AI model carries security risks. Those guides suggest using restricted roles, read-only databases, and sandboxes to lower your risk. Once you create the read-only user in MongoDB, you pass those login details to LlamaIndex. Your data reader or vector store setup then uses that user for every query.

What native roles solve and what they miss

A native read role solves a big problem for teams building with AI. It stops the agent from running write commands or dropping collections. Even if a user tricks the model with a prompt, the database blocks any write. The database engine checks the role on login and rejects any disallowed action right away.

Still, database roles only decide who can log in and which collections they can read. A read role does not inspect what happens inside an allowed find command in flight. If a collection holds customer records, the read role returns every field in matching documents. That means emails, phone numbers, card numbers, and national IDs flow straight to the model. Data masking is needed to rewrite matched fields in results before the app processes them.

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.

A built-in read role also cannot tell which human or service triggered a specific agent query. Every query from the LlamaIndex app uses the same shared database login. If an agent runs a heavy query that scans a whole collection, you lack clear logs. You can see the shared app user in the database log, but not the true caller. You also cannot require a quick human check before the agent runs a broad query.

Keeping agent database keys safe in production

When you connect an AI framework to a data store, key storage matters a lot. Many developers put the database login string in a local file or an app variable. If an attacker reads the app host, they can steal that static login key. With that key, they can pull every document that the read role allows.

Teams should rotate keys often and narrow roles to the smallest set of collections. Even then, static keys sitting on app servers remain a target for attackers. A strong setup pairs narrow database roles with strict controls over how the app connects. You want to check every query in flight and mask private fields in the results.

Add command guardrails and field masking beside MongoDB

Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev runs beside your database as a sidecar for your MongoDB. It checks every wire command and result document without changing your drivers or credentials.

Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run. Data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.

Frequently asked questions

What is the difference between the MongoDB read and readWrite roles?

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

Can I limit a LlamaIndex user to one MongoDB collection?

Yes, MongoDB lets you grant privileges down to the collection level. While the built-in read role covers a whole database, you can define a custom role for one collection.

Does a read-only role hide private fields from LlamaIndex?

No, a read role returns the full document for any collection the user can read. To hide emails, phone numbers, or card numbers, you must mask those fields in the query results.

The query runs. The emails stay hidden.