MongoDB Overview
What MongoDB is, how replica sets and the document model shape design, and the workloads it fits.
MongoDB is a document database. Data is stored as BSON documents in collections, indexed by field paths, and distributed through replica sets and optional sharding.
Architecture
- WiredTiger storage engine. Document-level concurrency, MVCC, snapshot isolation and compression by default.
- Replica set. One primary accepts writes; secondaries replicate the oplog and can serve reads. Members hold elections when the primary becomes unreachable. See Replica Sets.
- Oplog. A capped collection of idempotent operations, used for replication, change streams and point-in-time recovery.
- Sharding. Optional horizontal partitioning by a shard key, coordinated by config servers and
routed through
mongos. See Sharding.
Best use cases
- Aggregate-oriented data where a document is naturally read and written as a unit — orders with line items, product catalogues, user profiles with nested preferences.
- Schemas that vary between records or evolve frequently, where an
ALTER TABLEper change would be painful. - Workloads that scale by sharding on a key that appears in most queries.
- Applications that benefit from documents matching their in-memory object model.
When not to use it
- When the data is highly relational and queries join many entities. Joins exist (
$lookup) and are expensive; a relational engine does this better. - When you need cross-document invariants enforced by the database. Multi-document transactions exist, but they are more costly than a single-document update and should be the exception.
- For analytical scans over huge collections — a column store is the right tool.
Data model
{
_id: ObjectId("66aa1f2b9c4e2f0012ab34cd"),
customerId: ObjectId("66aa1e119c4e2f0012ab1122"),
status: "paid",
totalCents: 19900,
items: [
{ sku: "TS-001", qty: 2, priceCents: 4950 },
{ sku: "MG-014", qty: 1, priceCents: 10000 }
],
createdAt: ISODate("2026-07-31T10:22:00Z")
}Documents have a 16 MB limit, which is a design constraint rather than a technicality: it means unbounded arrays inside a document are a modelling error. See Document Modeling.
Consistency and transactions
Single-document writes are atomic. Multi-document transactions are available on replica sets and sharded clusters, with a default 60-second limit and higher cost — model to avoid needing them.
Consistency is tunable per operation through read and write concerns, which is where most of the durability decisions actually live.