How to Configure Dify MongoDB Read Only Access

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up dify mongodb read only access by creating a dedicated database user. You assign the built-in read role to that user before adding it to Dify. Teams often connect AI apps using default admin accounts to move fast. Those broad roles let an agent change or delete data by mistake. A read-only setup stops write actions at the database level. How dify mongodb read only access works in native roles MongoDB uses role-based rules to control what a user can do. A role grants specific actions

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 dify mongodb read only access by creating a dedicated database user. You assign the built-in read role to that user before adding it to Dify. Teams often connect AI apps using default admin accounts to move fast. Those broad roles let an agent change or delete data by mistake. A read-only setup stops write actions at the database level.

How dify mongodb read only access works in native roles

MongoDB uses role-based rules to control what a user can do. A role grants specific actions on defined resources in a target database. These rules can scope permissions down to a single collection level. Self-hosted databases and MongoDB Atlas have different built-in roles. Still, both systems build their roles from the same core privilege actions.

To keep Dify safe, you assign the built-in read role to its user. The read role lets the app read all non-system collections in that database. It permits actions like finding documents, listing collections, and checking index stats. It also allows watching change streams and reading the system JavaScript collection. Crucially, the read role leaves out insert, update, remove, and drop actions.

Many teams use the readWrite role by mistake during setup. That role includes everything in the read role plus data change powers. A user with readWrite can insert documents, remove records, and drop collections. When Dify runs a custom tool or workflow, the database checks the user role. If the user only has the read role, MongoDB blocks any write command.

What native roles solve and what they leave open

Native roles are great for stopping unauthorized changes to your stored records. If an AI workflow tries to delete a collection, the database says no. You can even create a custom role for tighter control. That role can limit Dify to reading just one or two safe collections. This setup decides which user can log in and which collections they see.

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.

Still, a database role cannot inspect what individual queries do in flight. Once a user has read rights on a collection, it can read every field. If a document holds emails, phone numbers, or card numbers, the query returns them. The database sends those raw fields straight into the Dify workflow context. Native read roles do not rewrite matched fields in query results.

Shared app credentials also make auditing hard for security teams. Every query from Dify arrives at the database under the same service user name. Your database logs show the shared account, not the person who triggered the prompt. Also, storing static database passwords inside app settings creates a secret management chore. Rotating those keys requires updating both the database and your AI app tools.

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. It reads the MongoDB wire protocol and checks commands and bulk writes as JSON while passing login through untouched.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents that match your rules. Every command and 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 and view collection stats without changing data. The readWrite role adds permissions to insert, update, remove, and drop collections or indexes.

Can I limit Dify to reading a single MongoDB collection?

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

Does a MongoDB read role hide sensitive fields like emails or card numbers?

No, the native read role grants read access to the whole document in a collection. To hide emails, phone numbers, or national IDs, you need data masking that rewrites results.

The query runs. The emails stay hidden.