Configuring n8n MongoDB Atlas nodes with custom roles

Published
Length
4 min read

Luis Felipe Dias

All posts

Connect the n8n MongoDB node to an Atlas cluster using a database user assigned to a custom role that limits actions to specific collections, and use views to hide fields the agent should not see. This setup stops the node from running destructive commands and restricts the data it can retrieve, but it does not rewrite the values in the documents that come back. You might be wondering if a read-only role is enough to protect customer emails or credit card numbers when an n8n agent processes dat

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.

Connect the n8n MongoDB node to an Atlas cluster using a database user assigned to a custom role that limits actions to specific collections, and use views to hide fields the agent should not see. This setup stops the node from running destructive commands and restricts the data it can retrieve, but it does not rewrite the values in the documents that come back.

You might be wondering if a read-only role is enough to protect customer emails or credit card numbers when an n8n agent processes data. The short answer is no. A role controls which collections the node can touch, but it does not change what is inside those collections. If the node has find access on a collection, it returns every field in every document that matches the query, including sensitive data.

How n8n connects to MongoDB Atlas

n8n includes a MongoDB node that uses the official MongoDB Node driver. This node can be used as a tool for an AI agent, where the model fills in parameters like the query filter or the collection name. The node supports document operations such as find, aggregate, insert, update, and delete. When you use the node in an n8n AI Agent workflow, the agent decides which operations to call and what parameters to pass.

The connection relies on a MongoDB Atlas database user. Atlas manages these users through its own interface, not through shell commands. You assign roles to the user to define what it can do. The node sends commands to the cluster using the credentials you provide, and the cluster enforces the permissions associated with that user.

Using custom roles to limit access

Built-in roles like read or readWrite apply to all non-system collections in a database. For an agent that only needs to work with a few specific collections, a custom role is a better fit. You can create a custom role that grants actions only on the collections the agent needs. For example, you can grant find on the products collection and nothing else. This prevents the agent from querying unrelated data or running administrative commands.

Custom roles can also restrict actions to a specific collection within a database. This is useful when you want the agent to read data but not modify it. You can grant find without insert, update, or delete. If the agent tries to run a write operation, the cluster rejects it because the role does not include the necessary privilege. This is a solid first step, but it only controls which commands are allowed, not what the commands return.

Restricting fields with views

MongoDB does not have column-level grants. You cannot create a role that says allow find on the users collection but hide the email field. To restrict fields, you use views. A view is a read-only object defined by an aggregation pipeline. You can create a view on a collection that excludes sensitive fields, such as email addresses or phone numbers. The agent then queries the view instead of the source collection.

To make this work, the agent needs find access on the view, not on the source collection. If the agent has find on the source collection, it can bypass the view and read the full data. You can also use the $redact stage in an aggregation pipeline to hide fields based on conditions in the documents themselves. However, this requires the query to pass the right parameters, and it is not a static restriction. Views are a better fit for permanent field hiding.

MongoDB Authentication & Authorization + Lambda Execution Roles: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What roles and views leave open

Even with a custom role and a view, the data that comes back is still the data in the database. If the view includes a field that contains a credit card number, the agent sees that number. If the agent queries a collection that has a field with a social security number, the agent sees that number.

This is a gap that matters when agents process personal data. An agent that reads a customer email and passes it to a language model for summarization is sending that email to the model provider. If the agent reads a credit card number and includes it in a log or a response, that number is now in a place it should not be. The control you need is not just about who can connect, but about what the agent sees when it connects.

Adding runtime masking on the connection

This is where hoop.dev fits in. hoop.dev is an open-source proxy that runs next to your MongoDB cluster and reads every command the driver sends and every document the server returns. It is a sidecar for your MongoDB, meaning it sits on the connection without replacing your database or changing your setup. The n8n node points to the hoop.dev proxy instead of the Atlas cluster directly. The agent changes one thing: the host and port in its connection string. The credentials stay the same, and the node works exactly as before.

hoop.dev rewrites sensitive values in the documents that come back, in memory, before they reach the n8n node. It uses Alcatraz, an open-source PII detector, to find emails, phone numbers, credit cards, and other sensitive types. You configure rules that say mask all email addresses or redact all credit card numbers.

hoop.dev also checks every command before it reaches the cluster. You can set guardrails that refuse destructive commands like drop or delete, even if the role allows them. This adds a layer of protection on top of your custom roles. If the agent tries to run a command that your rules do not allow, hoop.dev refuses it and returns an error that the agent can read. The agent knows why the command was refused, so it can adjust its approach.

Putting it all together

Start with a custom role that limits the n8n node to the collections it needs. Use views to hide fields that should never be exposed. Then add hoop.dev on the connection to mask sensitive values that your rules cover. This gives you defense in depth. The role stops the agent from touching the wrong data. The view hides the fields you know are sensitive.

hoop.dev is open source and MIT licensed. It generates evidence for your security program by recording every command and every decision.

For setup, see the MongoDB configuration docs and the data masking guide. The hoop.dev GitHub repository has the source code and examples if you want to run it locally.

Frequently asked questions

Does hoop.dev replace MongoDB roles? No. hoop.dev works next to your roles, not instead of them. Your roles still control which commands the cluster accepts. hoop.dev adds a check on the connection that can refuse commands and mask results before they reach the client. You keep your existing setup and add hoop.dev on top.

Can hoop.dev mask fields that are not in the built-in PII types? Yes. You can write rules that match specific field names.

Does hoop.dev work with n8n AI Agent nodes? Yes. The agent does not need to know hoop.dev is there. It just connects to the proxy, and hoop.dev handles the rest.

The query runs. The emails stay hidden.