How to Configure LlamaIndex MongoDB Permissions
You set up llamaindex mongodb permissions by assigning a scoped database role to the agent user. You then restrict that role to the specific collections your app needs to read. Many teams start by giving their AI app a broad read and write role. That setup lets the app run fast during testing. Still, broad roles expose every collection in the database to model errors. If an agent gets a bad prompt, it could change or delete records. Setting strict rules at the database level prevents unwanted wr
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 permissions by assigning a scoped database role to the agent user. You then restrict that role to the specific collections your app needs to read. Many teams start by giving their AI app a broad read and write role. That setup lets the app run fast during testing. Still, broad roles expose every collection in the database to model errors. If an agent gets a bad prompt, it could change or delete records. Setting strict rules at the database level prevents unwanted writes before they happen.
How llamaindex mongodb permissions work with database roles
MongoDB controls access 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 narrow access down to a single collection. Note that MongoDB Atlas users have different built-in roles than self-hosted users. Still, both setups use the exact same set of privilege actions.
For most search and retrieval apps, the built-in read role is the starting point. The read role lets the user read all non-system collections in that database. It allows actions like finding documents, listing collections, listing indexes, and checking stats. It does not allow the user to insert, update, or remove records. It also blocks the user from dropping a collection.
Some workflows need to store vectors or chat history back into the database. Those apps often use the read and write role instead. This role includes everything in the read role plus the right to change data. It allows the user to insert, update, and remove documents. It also lets the user create or drop collections and indexes. Because this role can drop data, you should scope a custom role to one collection.
Why native roles are only the first step for AI agents
LlamaIndex docs warns that running model-generated queries carries a real security risk. Their guides advise teams to take strong steps like using restricted roles and read-only databases. Without an explicit list of targets, a query engine can pull all schema context. That same risk applies when an agent queries document stores.
Database roles decide who can log in and which collections they can touch. Still, native roles do not check what an individual query does in flight. A read role stops an agent from deleting a collection. Yet that same read role still returns every field inside a permitted collection. If a document holds emails, phone numbers, or card numbers, the query returns them. The model then reads those raw values and may place them into an answer.
Static database credentials also create operational work for engineering teams. Apps usually store a long-lived connection string in a local settings file. If multiple agents share that user, your database logs show one single account. You cannot tell which user request or prompt triggered a heavy find action. When a developer leaves the team, rotating that shared secret requires an app update.
MongoDB Authentication & Authorization + AI Agent Permissions: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Scoping collection access for retrieval and vector search
To reduce risk, split your AI workload into separate database users. Create one user with read-only permissions for your retrieval query engine. Scope that custom role so it can only run find actions on public collections. Keep internal billing or account collections out of its reach.
If your app builds embeddings and writes them to a vector collection, make a second user. Give that second user write rights on the vector collection alone. Do not grant it the right to drop collections or change indexes in production. Run index creation steps through your deployment pipeline instead of the app user. This split ensures that a prompt flaw in the chat path cannot alter stored data.
Even with separate users, collection rules cannot filter sensitive fields inside allowed documents. When customer records mix public text with national IDs or emails, roles allow the whole document. Teams need a way to inspect queries and rewrite sensitive fields in results.
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
Should a LlamaIndex query engine use the built-in readWrite role?
No, a query engine that only fetches context should use a read-only role. The built-in readWrite role allows the user to remove data and drop collections. Use a custom role that grants read actions only on the collections the agent needs.
Can MongoDB roles limit access to specific collections?
Yes, MongoDB roles apply to a database and can grant access down to the collection level. You can build a custom role that allows find actions on one collection while blocking others. This stops the agent from reading unrelated tables in the same database.
How do you handle sensitive fields like emails in allowed collections?
Database roles work at the database and collection level, so they return the full document. To protect emails, phone numbers, or card numbers, use data masking to rewrite matched fields in results before the model processes them.
Related guides
In this post