/ Docs

MongoDB Adapter

Overview

The MongoDB adapter connects to a MongoDB server for durable, document-oriented storage. It supports all Bounda storage concerns and is a production-ready alternative to PostgreSQL.

Installation

npm i @bounda-dev/adapter-mongodb

Once installed, the adapter is discovered automatically at runtime via Bounda’s adapter discovery mechanism.

Supported Features

FeatureSupported
Event StoreYes
Read ModelYes
Policy DLQYes
Process Manager DLQYes
SchedulingYes

When to Use

  • Applications with MongoDB infrastructure — Reuse your existing MongoDB deployment.
  • Document-oriented read models — Filtering, sorting, pagination, and nested document queries.
  • Production deployments — Full durability and concurrent access.

Configuration

Every MongoDB configuration requires a url (connection string) and a dbName (database name). Bounda creates the collections and indexes automatically on first boot.

Event Store

domain: {
  user: {
    eventStore: {
      type: "mongodb",
      url: process.env.MONGODB_URL,
      dbName: "bounda",
    },
  },
}

Read Model

read: {
  "users-directory": {
    type: "mongodb",
    url: process.env.MONGODB_URL,
    dbName: "bounda",
  },
}

Scheduling

domain: {
  user: {
    scheduling: {
      type: "mongodb",
      url: process.env.MONGODB_URL,
      dbName: "bounda",
    },
  },
}

Process Manager DLQ

domain: {
  user: {
    processManagers: {
      dlq: {
        type: "mongodb",
        url: process.env.MONGODB_URL,
        dbName: "bounda",
      },
    },
  },
}

Policy DLQ

domain: {
  user: {
    policies: {
      dlq: {
        type: "mongodb",
        url: process.env.MONGODB_URL,
        dbName: "bounda",
      },
    },
  },
}
OptionTypeDescription
type"mongodb"Adapter type
urlstringMongoDB connection URL (e.g., mongodb://user:pass@host:27017)
dbNamestringDatabase name

Query API

Queries against a MongoDB read model have access to MongoDB-style operations:

export const handler: Query.HandlerFunction = async ({ db }) => {
  const users = await db
    .collection("users")
    .find({ status: "active" })
    .sort({ createdAt: -1 })
    .skip(0)
    .limit(10)
    .toArray();

  return users;
};

Environment Variables

Using environment variables for connection details keeps secrets out of your codebase:

import type { Config } from "@bounda-dev/config";

export default {
  domain: {
    user: {
      eventStore: {
        type: "mongodb",
        url: process.env.MONGODB_URL,
        dbName: "bounda",
      },
      scheduling: {
        type: "mongodb",
        url: process.env.MONGODB_URL,
        dbName: "bounda",
      },
      processManagers: {
        dlq: {
          type: "mongodb",
          url: process.env.MONGODB_URL,
          dbName: "bounda",
        },
      },
    },
  },
  read: {
    "users-directory": {
      type: "mongodb",
      url: process.env.MONGODB_URL,
      dbName: "bounda",
    },
  },
} satisfies Config;

Testing

Integration tests for the MongoDB adapter use TestContainers to spin up a real mongo:7 instance. A single container is shared across all test suites via vitest’s globalSetup; each suite gets its own database for isolation.

To run the tests locally, Docker must be running:

npm test --workspace=@bounda-dev/adapter-mongodb