MongoDB
DocsDatabasesMongoDB

MongoDB

Managed MongoDB with replica sets, automated backups, point-in-time recovery, and private cluster networking.

Powered by DocumentDB

MongoDB on StackBlaze is served by DocumentDB, a MongoDB wire-protocol–compatible engine built on PostgreSQL. You connect with the standard MongoDB drivers, connection strings, and tools (mongosh, Compass, Mongoose, Prisma, Motor), and everything in this guide applies as written. We use DocumentDB so we can ship a MongoDB-compatible database under a permissive license.

Creating a MongoDB instance

From the dashboard, click New serviceDatabaseMongoDB. Choose MongoDB 7.x, a storage size (1 GB to 500 GB), and a plan. StackBlaze provisions a StatefulSet and initializes the replica set automatically.

Attach MongoDB to your service from the Environment tab. StackBlaze injects MONGODB_URL automatically.

Connection strings

Internal (recommended)

Connection string (internal, single-node)
mongodb://user:password@[service-name].internal:27017/dbname

Replica set connection (Pro+)

On Pro and Enterprise plans, MongoDB runs as a 3-node replica set. Use the replica set connection string to enable automatic failover and read preference configuration.

Connection string (replica set)
mongodb://user:password@[service-name]-0.internal:27017,[service-name]-1.internal:27017,[service-name]-2.internal:27017/dbname?replicaSet=rs0&authSource=admin

External (for Compass / mongosh)

Use the external connection string for MongoDB Compass or mongosh from your local machine. Find it in Settings → Connection.

mongosh (external)
mongosh "mongodb+srv://user:password@cluster.stackblaze.cloud/dbname"

Tip

The mongodb+srv URI format uses DNS SRV records and automatically discovers all replica set members. Prefer it over manually listing hostnames when connecting external tools.

Replica sets

PlanTopologyFailover
FreeSingle node (no replica set)None
StarterSingle node (no replica set)None
Pro3-node replica setAutomatic (<30s)
Enterprise3-node replica set + hidden secondaryAutomatic (<30s)

With a 3-node replica set, MongoDB automatically elects a new primary if the current primary becomes unavailable. Your application should use the replica set connection string and enable retryable writes for seamless failover.

Mongoose connection (with retryable writes)
import mongoose from 'mongoose'

await mongoose.connect(process.env.MONGODB_URL, {
  retryWrites: true,
  w: 'majority',
  readPreference: 'primaryPreferred',
})

Automated backups

All plans include daily automated snapshots using mongodump for Free/Starter and filesystem-level snapshots for Pro/Enterprise. Backups are encrypted with AES-256 and stored in a separate availability zone.

Point-in-time recovery (PITR)

Pro and Enterprise plans support PITR via continuous oplog tailing. StackBlaze ships oplog entries to object storage, enabling recovery to any point within the retention window.

PlanPITRRetention window
FreeNo -
StarterNo3 days (snapshots only)
ProYes7 days
EnterpriseYes30 days

Indexes

Create indexes in your application startup code or migration scripts. For production deployments, always create indexes with { background: true } (MongoDB < 4.2) or as a rolling build to avoid blocking reads and writes on large collections.

Create indexes (Mongoose)
// In your model definition
const userSchema = new mongoose.Schema({
  email: { type: String, unique: true, index: true },
  createdAt: { type: Date, index: true },
  'profile.city': { type: String },
})

// Compound index
userSchema.index({ 'profile.city': 1, createdAt: -1 })

// Text search index
userSchema.index({ name: 'text', bio: 'text' })

Connecting with common ODMs

Mongoose

db.ts
import mongoose from 'mongoose'

if (mongoose.connection.readyState === 0) {
  await mongoose.connect(process.env.MONGODB_URL!, {
    serverSelectionTimeoutMS: 5000,
    socketTimeoutMS: 45000,
  })
}

Prisma (MongoDB provider)

prisma/schema.prisma
datasource db {
  provider = "mongodb"
  url      = env("MONGODB_URL")
}

model User {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  email String @unique
  name  String
}

Motor (Python async)

database.py
from motor.motor_asyncio import AsyncIOMotorClient
import os

client = AsyncIOMotorClient(os.environ["MONGODB_URL"])
db = client.get_default_database()

Under the hood

MongoDB runs as a Kubernetes StatefulSet. Each node gets its own PersistentVolumeClaim for data and journal files. On Pro+, a 3-node replica set is initialized automatically at provision time using rs.initiate(). Stable per-pod DNS names ([service-name]-0.internal, [service-name]-1.internal, etc.) are provided by a Kubernetes Headless Service, which is required for replica set member addressing.