MongoDB
Managed MongoDB with replica sets, automated backups, and private cluster networking.
Creating a MongoDB instance
Section titled “Creating a MongoDB instance”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 strings
Section titled “Connection strings”Internal (recommended)
Section titled “Internal (recommended)”mongodb://user:password@[service-name]:27017/dbnameReplica set connection (Pro+)
Section titled “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.
mongodb://user:password@[service-name]-0:27017,[service-name]-1:27017,[service-name]-2:27017/dbname?replicaSet=rs0&authSource=adminExternal (for Compass / mongosh)
Section titled “External (for Compass / mongosh)”Use the external connection string for MongoDB Compass or mongosh from your local machine. Find it in Settings → Connection.
mongosh "mongodb+srv://user:password@cluster.stackblaze.cloud/dbname"Replica sets
Section titled “Replica sets”| 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.
import mongoose from 'mongoose'
await mongoose.connect(process.env.MONGODB_URL, { retryWrites: true, w: 'majority', readPreference: 'primaryPreferred',})Automated backups
Section titled “Automated backups”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 |
Indexes
Section titled “Indexes”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.
// In your model definitionconst userSchema = new mongoose.Schema({ email: { type: String, unique: true, index: true }, createdAt: { type: Date, index: true }, 'profile.city': { type: String },})
// Compound indexuserSchema.index({ 'profile.city': 1, createdAt: -1 })
// Text search indexuserSchema.index({ name: 'text', bio: 'text' })Connecting with common ODMs
Section titled “Connecting with common ODMs”Mongoose
Section titled “Mongoose”import mongoose from 'mongoose'
if (mongoose.connection.readyState === 0) { await mongoose.connect(process.env.MONGODB_URL!, { serverSelectionTimeoutMS: 5000, socketTimeoutMS: 45000, })}Prisma (MongoDB provider)
Section titled “Prisma (MongoDB provider)”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)
Section titled “Motor (Python async)”from motor.motor_asyncio import AsyncIOMotorClientimport os
client = AsyncIOMotorClient(os.environ["MONGODB_URL"])db = client.get_default_database()