How to Configure n8n MongoDB Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up n8n mongodb least privilege by making a database user with scoped roles. Then you give that user key to your workflow nodes. Many teams take a fast path and reuse an admin database key in n8n. That broad role lets a broken workflow or AI agent change or delete collections by mistake. A small role limits what the workflow can read or write in your database. How to set up n8n mongodb least privilege in your database MongoDB uses role-based rules to control what a database user can do

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 n8n mongodb least privilege by making a database user with scoped roles. Then you give that user key to your workflow nodes. Many teams take a fast path and reuse an admin database key in n8n. That broad role lets a broken workflow or AI agent change or delete collections by mistake. A small role limits what the workflow can read or write in your database.

How to set up n8n mongodb least privilege in your database

MongoDB uses role-based rules to control what a database user can do. Each role grants a set of actions on a specific resource. A role works on the database where you define it. You can also scope custom roles down to a single collection. Note that MongoDB Atlas and self-hosted setups use different built-in role lists. Still, both build their roles from the same core action rules.

Start by checking what your n8n workflow needs to do. If the workflow only fetches data, assign the built-in read role. The read role lets the user run find, listCollections, listIndexes, dbStats, collStats, and changeStream actions. It covers all non-system collections in that database. It leaves out insert, update, remove, and dropCollection actions so the workflow cannot change records.

Some workflows need to add or edit documents. Even then, the built-in readWrite role is often too broad for production use. The readWrite role includes everything in the read role plus data edit actions. It allows insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Allowing dropCollection or remove in an automated workflow creates high risk. Instead, build a custom role that grants only find, insert, and update on target collections.

How n8n AI agents use database tools

Scoped roles matter even more when you build AI workflows in n8n. The n8n AI Agent node connects a chat model to one or more tool sub-nodes. The agent picks which tools to call to finish a task. You must attach at least one tool sub-node to an AI Agent node. From n8n 1.82.0 onward, the old agent type setting is deprecated. Every AI Agent node now runs as a Tools Agent. Node version 1 is removed in n8n 3.0.

Teams can connect database sub-nodes, a Call n8n Workflow Tool, or a Custom Code Tool. You can also attach an HTTP Request Tool or MCP servers to the agent. Because the chat model picks when to run a tool, its queries can change each run. If the database user has dropCollection or remove rights, one bad prompt can wipe data. Giving each n8n credential a custom collection role stops those destructive actions at the database.

Least Privilege Principle + 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 MongoDB roles do a great job of blocking unauthorized collection actions at login. A read-only user cannot drop a collection or delete documents. A collection-scoped role keeps a workflow out of other tables in the same database. This setup cuts down the blast radius of a leaked n8n key or bad prompt.

Still, database roles only check who logs in and which collection they touch. Native roles do not inspect what individual queries return in flight. If an allowed collection holds emails, phone numbers, card numbers, or national IDs, find returns them. The chat model or workflow log then gets every sensitive field in plain text. Also, teams that share one n8n credential cannot see which person or run sent a query. Nor can you pause a single risky update for human approval before it runs.

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 I give my n8n workflow the MongoDB readWrite role?

No, avoid the built-in readWrite role for most automated workflows. It grants dropCollection, dropIndex, and remove actions along with insert and update. Create a custom role with only the exact actions your workflow needs on specific collections.

How does the n8n AI Agent node run database actions?

The AI Agent node links a chat model with one or more tool sub-nodes. Since n8n 1.82.0, all AI Agent nodes work as a Tools Agent. The agent chooses which attached tool sub-node or workflow tool to call to finish its task.

Do collection-level MongoDB roles hide sensitive fields from n8n?

No, collection roles only control which actions a user can run on a collection. If a user has the find action, queries return full documents from that collection. You need data masking in flight to rewrite emails, phone numbers, or card numbers in results.

The query runs. The emails stay hidden.