Skip to main content
[ PIER ]

MongoDB

The document database for flexible, evolving schemas.

Database Cluster-ready #nosql#document

MongoDB is a document-oriented NoSQL database that stores data as BSON (binary JSON). It's the default choice for applications with rapidly evolving schemas, deeply nested data, and high-volume writes — where rigid SQL DDL would slow development down.

Deploy with Pier

  1. 1 Open the Pier dashboard and click Add service.
  2. 2 Pick MongoDB from the template list.
  3. 3 Choose the version, set a service name, and Pier provisions the container, storage, and ports automatically.
  4. 4 Attach a domain if you want HTTPS. Traefik auto-provisions the Let's Encrypt certificate.

What is MongoDB?

MongoDB is the most-deployed NoSQL document database. Instead of tables and rows, it stores BSON documents — nested objects much like JSON — grouped into collections. Schemas are optional; queries use a rich JSON-like syntax that maps naturally to JavaScript and other dynamic languages.

The trade-off vs SQL is real: you give up cross-collection foreign-key integrity and SQL-standard ANSI compliance. In exchange you get schema-on-read flexibility, deep document atomicity, and a query language that fits the shape of modern app data better than rigid relational rows.

How Pier deploys it

Pier uses the official mongo Docker image, mounting /data/db as the persistent volume. The default version is latest (MongoDB 8.x); 7 and 6 are available for strict version pinning. Root credentials are auto-generated via MONGO_INITDB_ROOT_USERNAME + MONGO_INITDB_ROOT_PASSWORD.

Single-node only in this template — multi-node replica sets require manual initialization and aren’t supported via Pier’s cluster mode yet. Backups use mongodump --archive --gzip on schedule and can push to S3.

When NOT to use MongoDB

For workloads with rigid relational integrity needs (financial ledgers, inventory with strict foreign keys) — pick Postgres. For full-text search — Elasticsearch. For embedded single-process — SQLite. Mongo shines when your data has nested structure that changes shape often and you want JSON-native ergonomics.

Key features

Schema flexibility

Every document in a collection can have its own shape. Add fields without ALTER TABLE; remove them without migrations. Validation rules can be enforced when needed.

Powerful aggregation pipeline

$match, $group, $lookup (join), $facet, $bucket, $unwind, $graphLookup — MongoDB's aggregation framework rivals SQL for analytical queries on document data.

Native horizontal scaling

Sharding by hashed or ranged key splits collections across nodes. Replica sets give automatic failover, read scaling, and zero-downtime maintenance.

Geospatial indexes

2dsphere indexes for GeoJSON points, polygons, multipolygons. $near, $geoWithin, $geoIntersects queries with kilometer-level accuracy.

Change streams (CDC)

Subscribe to inserts/updates/deletes in real time via change streams. Build reactive UIs, sync to other systems, drive event-driven workflows.

Time series collections (5.0+)

Dedicated time series collections with columnar storage and automatic bucketing — purpose-built for IoT and metrics workloads.

Use cases

User-generated content platforms

Social media posts, comments, reactions — flexible per-document fields fit naturally into BSON.

Real-time IoT data ingest

High write throughput, time-series collections, and change streams make Mongo a viable purpose-built IoT backend.

Product catalog with variable attributes

E-commerce catalogs with different attributes per category (TVs have screen size; books have ISBN) — no need for sparse SQL columns or EAV.

Mobile app sync backend

Mongo Realm + Atlas Device Sync. For self-hosted, pair with WatermelonDB or PouchDB-style sync layers.

Single view / 360 customer profiles

Aggregate data from many systems into one denormalized document per customer. Atomic updates on nested fields.

Code examples

Insert a document javascript
db.users.insertOne({
  email: "[email protected]",
  profile: {
    name: "Aleksandr",
    locale: "en",
    tags: ["beta", "pro"]
  },
  createdAt: new Date()
});
Aggregation pipeline (top tags per locale) javascript
db.users.aggregate([
  { $unwind: "$profile.tags" },
  { $group: {
      _id: { locale: "$profile.locale", tag: "$profile.tags" },
      count: { $sum: 1 }
  }},
  { $sort: { count: -1 } },
  { $limit: 20 }
]);
Geospatial query (find within 5 km) javascript
db.places.createIndex({ location: "2dsphere" });

db.places.find({
  location: {
    $near: {
      $geometry: { type: "Point", coordinates: [37.6173, 55.7558] },
      $maxDistance: 5000
    }
  }
});
Change stream (real-time CDC) javascript
const stream = db.orders.watch([
  { $match: { "fullDocument.status": "paid" } }
]);
stream.on("change", (ev) => sendToWarehouse(ev.fullDocument));

How it compares

vs PostgreSQL with JSONB Postgres's JSONB matches Mongo's flexibility while keeping joins, transactions, and SQL. If you need both relational data and documents in one place, Postgres often wins. Pick Mongo when documents are the primary model and you want change streams + sharding native.
vs CouchDB CouchDB is master-master with offline-first sync — great for mobile. Mongo wins on aggregation power, ecosystem, and tooling.
vs Elasticsearch Elastic is purpose-built for full-text search; Mongo is for general document storage. They often pair — Mongo for source of truth, Elastic for search indices.
vs DynamoDB Dynamo is AWS-managed and pay-per-request; Mongo is self-hosted with predictable costs. Mongo has aggregations Dynamo lacks.

Frequently asked questions

Which MongoDB version does Pier deploy?
Default is `latest` (currently 8.x). Versions 7 and 6 are also in the selector. MongoDB 5.0+ is required for time series collections.
Is replication supported?
Single-node by default. Replica sets need 3+ nodes with special init scripts — not supported via the standard template. For replica sets, use a custom Docker template or Mongo Atlas.
Does the SSPL license affect me?
SSPL only restricts offering MongoDB as a SaaS to third parties. Using it as the database for your own application — even a commercial one — is fine. Pier deploys the official `mongo` image directly.
How do I connect from my app?
Standard URI — `mongodb://user:pass@host:27017/dbname`. Pier shows the full string on the service detail page.
Default authentication?
Pier sets `MONGO_INITDB_ROOT_USERNAME` and `MONGO_INITDB_ROOT_PASSWORD` (auto-generated strong password) on init.
Are backups automatic?
Yes — `mongodump --archive --gzip` runs on schedule and can ship to S3. Per-database and full-instance backups supported.
When should I add indexes?
As soon as a query takes over 100ms. Use `db.collection.explain('executionStats').find(...)` to inspect plans. Most production Mongo problems are missing indexes on $sort or $match fields.

Related services

Deploy on your VPS

MongoDB is a document-oriented NoSQL database that stores data as BSON (binary JSON). It's the default choice for applications with rapidly evolving schemas, deeply nested data, and high-volume writes — where rigid SQL DDL would slow development down.

Deploy this service →