Skip to content

MongoDB

Managed MongoDB with replica sets, automated backups, and private cluster networking.

From the dashboard, click New service → Database → MongoDB. 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 string (internal, single-node)
mongodb://user:password@[service-name]:27017/dbname

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:27017,[service-name]-1:27017,[service-name]-2:27017/dbname?replicaSet=rs0&authSource=admin

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"
Plan Topology Failover
Free Single node (no replica set) None
Starter Single node (no replica set) None
Pro 3-node replica set Automatic (under 30s)
Enterprise 3-node replica set + hidden secondary Automatic (under 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',
})

All plans include daily automated snapshots, and you can take an on-demand snapshot at any time before a risky change. Backups are encrypted at rest and stored separately from your primary data. Restore to a new instance without interrupting your live database from Backups → Restore.

Plan Snapshot retention
Free 1 day
Starter 3 days
Pro 7 days
Enterprise 30 days

Create indexes in your application startup code or migration scripts. For production deployments, always create indexes with { background: true } (MongoDB before 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' })
db.ts
import mongoose from 'mongoose'
if (mongoose.connection.readyState === 0) {
await mongoose.connect(process.env.MONGODB_URL!, {
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
})
}
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
}
database.py
from motor.motor_asyncio import AsyncIOMotorClient
import os
client = AsyncIOMotorClient(os.environ["MONGODB_URL"])
db = client.get_default_database()