How to Configure LlamaIndex MongoDB Credentials
You set up llamaindex mongodb credentials by creating a scoped database user and passing its connection string to your reader or vector store. Your app reads that string from a secret store when the worker starts up. Many teams start by reusing a broad admin user during early tests. That habit creates a large risk once an AI agent runs in production. If an agent holds write rights, a bad prompt can alter or delete records. Scoping the database user before launch is the first step toward safe da
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 credentials by creating a scoped database user and passing its connection string to your reader or vector store. Your app reads that string from a secret store when the worker starts up.
Many teams start by reusing a broad admin user during early tests. That habit creates a large risk once an AI agent runs in production. If an agent holds write rights, a bad prompt can alter or delete records. Scoping the database user before launch is the first step toward safe data access.
How llamaindex mongodb credentials work with database roles
LlamaIndex connects to MongoDB through a standard client driver inside your Python code. The app loads the database URI from your secret store at runtime. Then the driver logs in and runs commands on behalf of your agent.
MongoDB controls access through roles that grant sets of actions on defined resources. A role applies to the database where you define it. You can also scope a role down to the collection level of detail. Note that self-hosted deployments and MongoDB Atlas have different built-in role lists. Still, both setups build their roles from the same set of privilege actions.
When an agent only needs to fetch documents, assign the built-in read role. The read role grants access to all non-system collections in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It leaves out actions that change data, such as insert, update, remove, or dropCollection.
Some workflows store chat history or vector embeddings back into the database. Those jobs use the readWrite role instead of the basic read role. The readWrite role includes every action in the read role plus data change rights. It adds insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection actions. Because dropCollection is part of readWrite, a single shared login carries real risk.
Split reader and writer roles for agent tasks
A safe setup splits your database users by the exact task they perform. Your data ingestion pipeline gets one user with write access to the vector collection. Your query engine gets a second user that only holds read permissions.
Ephemeral Credentials + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
You can go further and build a custom role for the query agent. That custom role can restrict the find action to one or two collections. If the database holds a billing collection, the agent role has no access there. This matches the core security advice in the LlamaIndex docs for query engines. The docs urge teams to use restricted roles, read-only databases, and sandboxing.
What native database roles solve and what they miss
Native database roles do a great job of blocking broad collection changes. A read-only user stops an agent from dropping collections or changing stored records. Scoping a role to specific collections also keeps whole tables out of reach.
Still, a database login only checks who connects and which collection they touch. It does not inspect the contents of each document returned by a query. If an allowed collection holds emails, phone numbers, or card numbers, the query returns them. The raw fields flow straight into the model prompt and your app logs.
Static connection strings in app settings also create long-term key sprawl. Developers often copy those strings into local files to debug an agent issue. When a user leaves the team, rotating shared keys across services takes hours. Database roles alone cannot mask live fields like national IDs or approve single queries.
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 is an open-source proxy that sits next to MongoDB as a sidecar for your MongoDB. It reads every command from mongosh, drivers, or agent tools.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines. Next, hoop.dev data masking rewrites sensitive values in returned documents based on your rules and logs each decision. Explore the repo on GitHub.
Frequently asked questions
Which MongoDB role should a LlamaIndex query agent use?
Assign the built-in read role or a custom role scoped to specific collections. The read role lets the agent run find Commands without insert, update, remove, or dropCollection rights.
Should ingestion scripts and query agents share the same login?
No, you should give ingestion jobs and query agents separate database logins. Ingestion jobs need readWrite actions, while query agents only need read access to fetch context.
Why is a read-only role not enough for sensitive collections?
A read role gives access to every field inside an allowed collection. If documents contain emails, phone numbers, or card numbers, the agent still pulls those raw values.
Related guides
In this post