vishal patel
Applied in productionAdvancedUpdated 2026-09-23

MongoDB at Enterprise Scale

Data modelling by access pattern, indexing (ESR rule), transactions, change streams and the operational gotchas of large multi-tenant collections.

mongodbindexingdata-modelingperformance

Model for access patterns

  • Embed what you read together and what's bounded (an entry's small metadata).
  • Reference what's unbounded or shared (assets, users, very large arrays).
  • Useful patterns: Subset (hot fields in the main doc), Bucket (time-series), Computed (pre-aggregated counts), Extended reference (copy a few fields to avoid lookups), Schema versioning (a _v field and lazy migration).

Indexing: the ESR rule

Order compound index fields as Equality → Sort → Range.

// Query: tenant's published entries in a content type, newest first, updated in last 30 days
db.entries.find({ stack: s, branch: b, contentType: ct, updatedAt: { $gte: d } })
          .sort({ updatedAt: -1 })
// Index: equality fields first, then sort/range
db.entries.createIndex({ stack: 1, branch: 1, contentType: 1, updatedAt: -1 })
diagram

Always check explain("executionStats"): keysExamined ≈ nReturned is the goal.

Operational gotchas at scale

  • Unbounded arrays and the 16 MB document limit.
  • Large $in or skip-based pagination: use range (keyset) pagination instead, e.g. _id > lastId.
  • Index bloat: every index slows writes. Audit usage with $indexStats.
  • Transactions: available on replica sets, but keep them short (under 1 s, few docs) and design aggregates to avoid them.
  • Bulk writes: use bulkWrite with ordered: false for throughput, and handle per-item errors (which fits nicely with per-item job status).
  • Tenant isolation: every index should start with the tenant key.

Sources & further learning

Videos, courses, docs and books I recommend for this topic.

Related topics