How to Configure n8n MongoDB Credentials
To set up n8n mongodb credentials, create a database user and add its login details to your workflow. You should assign that user a narrow database role before any node runs. Many teams start by reusing a broad admin login to get workflows running fast. That shortcut gives every workflow and agent tool full power to change or delete data. A bad prompt or broken loop can then damage production collections in seconds. How to set up n8n mongodb credentials with database roles A safe setup starts
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.
To set up n8n mongodb credentials, create a database user and add its login details to your workflow. You should assign that user a narrow database role before any node runs. Many teams start by reusing a broad admin login to get workflows running fast. That shortcut gives every workflow and agent tool full power to change or delete data. A bad prompt or broken loop can then damage production collections in seconds.
How to set up n8n mongodb credentials with database roles
A safe setup starts inside your database before you open your workflow tool. MongoDB controls access through role-based rules on a specific database. Each role grants a set of actions on defined resources. These rules can apply down to a single collection. Note that MongoDB Atlas users have different built-in roles than self-hosted users. Both deployment types still build their roles from the same basic privilege actions.
For workflows that only fetch data, assign the built-in read role to your user. The read role lets the user run find, listCollections, listIndexes, dbStats, collStats, and changeStream actions. It applies to all non-system collections in that database. It does not allow insert, update, remove, or dropCollection actions. This stops a reporting workflow from changing records by mistake.
Some workflows must save new records or edit existing documents. Those workflows use the built-in readWrite role on the target database. The readWrite role includes every action in the read role plus write actions. Those extra actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. If a workflow only needs one collection, define a custom role scoped to that collection. Once the user exists, store the connection details in your workflow credential store. You can then attach that credential to standard database nodes or AI Agent tool sub-nodes.
Why AI Agent workflows increase database risk
In n8n, an AI Agent node links a chat model to one or more tool sub-nodes. You must attach at least one tool sub-node for the agent to work. From n8n 1.82.0 onward, the old agent type setting is deprecated. Every AI Agent node now runs as a Tools Agent, and version 1 leaves in n8n 3.0. In this setup, the agent decides which tools to call to finish a task.
Teams can connect database tool sub-nodes, a Call n8n Workflow Tool, or a Custom Code Tool. They can also connect an HTTP Request Tool or MCP servers to the agent. When an agent calls a database tool, it uses the saved login for that node. The chat model picks the inputs based on user prompts and past steps. Because the model chooses when to run the tool, static logins create real risk.
Ephemeral Credentials + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What native database roles solve and what they miss
Native database roles decide which user can log in and which actions that account may run. Scoping a role to read actions stops an agent from dropping a collection. Scoping a role to one collection keeps other collections safe from stray queries. This setup forms a strong first line of defense for automated workflows.
Still, database roles check the account only when the session starts and runs an action. They do not inspect the content of individual commands or results in flight. A read role still returns every field inside a permitted collection. If documents hold emails, phone numbers, card numbers, or national IDs, the query returns them raw. Those sensitive fields then flow straight into workflow logs or external chat models.
The readWrite role leaves an even larger gap during live runs. It bundles safe insert and update actions with remove and dropCollection actions. A shared workflow credential also hides which human user or workflow triggered a specific query. The database log only shows the single service account name for every run. Teams cannot pause a single risky write command for human approval before it runs.
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 safest built-in role for read-only n8n workflows?
Use the built-in read role on the single database your workflow needs. It permits find and collection listing actions while blocking insert, update, remove, and dropCollection actions.
Why is the MongoDB readWrite role risky for AI Agent tools?
The readWrite role includes remove, dropIndex, and dropCollection alongside insert and update. Since the agent decides which tools to call, a bad prompt could delete records or collections.
Do MongoDB read roles hide sensitive fields from chat models?
No, a read role grants access at the database or collection level. Every field in a matched document returns to the workflow unless field masking rewrites the results.
Related guides
In this post