Home » n8n» Connect MongoDB to n8n: Docker and Node.js Guide

Connect MongoDB to n8n?

You can connect MongoDB to n8n by running MongoDB in Docker, creating a database user, and adding a MongoDB credential inside n8n using a valid MongoDB URI. This lets the n8n MongoDB node read and write collections from your workflows.

What You Need

  • Docker and docker-compose installed on your machine.
  • Basic n8n knowledge and access to the n8n web editor.
  • MongoDB image (official) and a database for workflows.
  • Optional: Node.js if you run n8n without Docker.

How to Connect MongoDB to n8n

This section shows a simple Docker setup, credentials creation, and an example workflow step using the MongoDB node.

Step 1: Start MongoDB with Docker

Run an official MongoDB container with a root user. Replace secrets before production.

docker run -d --name mongo --network n8n-net -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=ExampleRootPass \
  mongo:6

Step 2: Create a database user

Open a shell into the container and create an app user for n8n. Use strong passwords.

docker exec -it mongo mongosh -u admin -p ExampleRootPass --authenticationDatabase admin

# Inside mongosh session:
use mydb
db.createUser({
  user: "n8n_user",
  pwd: "StrongN8nPass",
  roles: [{ role: "readWrite", db: "mydb" }]
})
exit

Step 3: Start n8n with Docker

Run n8n on the same Docker network so it can reach MongoDB by service name.

docker network create n8n-net

docker run -d --name n8n --network n8n-net -p 5678:5678 \
  -e N8N_BASIC_AUTH_ACTIVE=true \
  -e N8N_BASIC_AUTH_USER=admin \
  -e N8N_BASIC_AUTH_PASSWORD=AdminPass \
  n8nio/n8n:latest

Step 4: Add MongoDB credentials in n8n

Create a new MongoDB credential in the n8n UI and use a connection URI that points to the Mongo service.

mongodb://n8n_user:StrongN8nPass@mongo:27017/mydb?authSource=admin

After saving credentials, add the MongoDB node in a workflow. Use operations like “find”, “insert” or “update” and select the credential you created.

Docker Compose example

Here is a minimal docker-compose.yml you can copy and use. Change passwords first.

version: '3.8'
services:
  mongo:
    image: mongo:6
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: ExampleRootPass
    ports:
      - 27017:27017

  n8n:
    image: n8nio/n8n:latest
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=admin
      - N8N_BASIC_AUTH_PASSWORD=AdminPass
    ports:
      - 5678:5678
    depends_on:
      - mongo
networks:
  default:
    name: n8n-net

Update

To update the containers, pull the latest images and recreate the services. Use safe downtime windows for production.

docker pull mongo:6
docker pull n8nio/n8n:latest
# If using docker-compose:
docker-compose pull
docker-compose up -d

Security

Use strong, unique passwords for both root and app users. Do not expose MongoDB to the public internet.

  • Run MongoDB and n8n on a private network or VPN.
  • Use TLS if connecting over untrusted networks.
  • Store n8n credentials securely inside n8n and avoid hard-coding secrets.
  • Limit database user roles to least privilege (readWrite only on the needed DB).

Done

After these steps, n8n can access MongoDB via the MongoDB node using your saved credential. Build workflows that read and write collections safely.


Neil
Written by Neil

Neil is a true n8n geek who lives and breathes workflow automation. He dives deep into nodes, triggers, webhooks, custom logic, and self-hosting setups, sharing everything he learns about n8n on AutomationCompare.com. As part of a broader team of automation specialists, Neil focuses purely on mastering n8n and helping others unlock its full potential.

Keep Reading

Scroll to Top