Home›Learn›MongoDB 2: how it stores your data
MongoDB · Chapter 2

How MongoDB stores your data

What is running, what is in memory, and which file holds my collection?

The whole machine before we follow a write: mongod, the WiredTiger cache, the journal, the oplog and the data files.

The architecture: processes, memory, disk

Process, memory, disk: a request enters at the process layer, memory serves it when it can, and disk makes it permanent. Name the layer first, then diagnose inside it. The full rule is on the PostgreSQL page: name the layer first, then diagnose inside it.

Listen to the whole architecture, 3 min 59 s

The same layers as a still diagram, with every part named:

MongoDB · WiredTiger: architecture

Request path

Processesmongod per member · the primary accepts writes, secondaries apply the oplog · mongos routers when sharded
→
MemoryWiredTiger cache (~50% of (RAM − 1 GB), minimum 256 MB) holding uncompressed data + the OS filesystem cache
→
Disk: the dbPathcollection-*.wt · index-*.wt · journal/WiredTigerLog.* · the oplog in local.oplog.rs
Who does what: The primary orders all writes into the oplog. WiredTiger checkpoints about every 60 s and makes the .wt files consistent. Secondaries tail the oplog and apply the same operations in the same order.
Files on disk · what ls actually shows

Same rule as PostgreSQL: there is no hidden state. Every file here is inspectable with ls and du while the server runs. The full version is on the PostgreSQL page.

MongoDB: the dbPath
$ ls /var/lib/mongo
WiredTiger        # WiredTiger version marker
WiredTiger.wt     # WT's own metadata: where the
                  # last checkpoint is
WiredTiger.turtle  # backup copy of that metadata
_mdb_catalog.wt   # THE MAP: "users" = which file?
collection-2-4471….wt # ← collection "users"
collection-5-9912….wt  # ← collection "orders"
index-3-4471….wt  # ← the _id index of "users":
                  #   one file per index
journal/          # THE JOURNAL (write-ahead log):
  WiredTigerLog.0000000123
mongod.lock       # lock file; a stale one means the
                  # last shutdown was not clean
sizeStorer.wt  storage.bson
diagnostic.data/   # metrics mongod records on itself
File names are internal IDs, not collection names. _mdb_catalog.wt holds the mapping. In mongosh, db.users.stats().wiredTiger.uri shows a collection's real file name.
The log files · one by one

Same pattern as PostgreSQL: five logs, one job each, and every operational budget is a time window. The full version is on the PostgreSQL page.

MongoDB · WiredTiger

Journal

journal/WiredTigerLog.0000000123
Where it sits
The journal/ folder inside the dbPath. A sequence of WiredTigerLog.* files, written by the WiredTiger storage engine.
What it does
WiredTiger's write-ahead log. Data files are only made consistent at checkpoints, roughly every 60 seconds. The journal covers the gap. A write acknowledged with j: true is durable once its journal entry is on disk. On restart mongod replays the journal from the last checkpoint.
What breaks without it
A crash could lose every write since the last checkpoint. Up to about 60 seconds of acknowledged data. The journal exists to close exactly that gap.
Look at it
$ ls /var/lib/mongo/journal/ -- in mongosh: db.serverStatus().storageEngine db.adminCommand({getCmdLineOpts: 1}) -- parsed.storage.journal shows the journal settings
MongoDB

Oplog: the replica set log

local.oplog.rs
Where it sits
Not a file. A capped collection named oplog.rs in the local database, present on every replica set member. Capped means fixed-size. Old entries fall off the front as new ones arrive.
What it does
An ordered record of every write on the primary, each stamped with a timestamp. Secondaries continuously tail it and apply the same operations in the same order. Its size defines the oplog window. That is how far behind a secondary can fall and still catch up.
What breaks without it
No replica set at all. And if a secondary stays down longer than the oplog window, the entries it missed are gone. It must copy everything again (initial sync).
Look at it
// in mongosh: rs.printReplicationInfo() db.getSiblingDB('local').oplog.rs .find().sort({$natural: -1}).limit(1)

Journal to checkpoint: what covers the 60-second gap

Writejournal/WiredTigerLog.*, on disk first
→
Cachetimestamped versions, dirty pages
→
Checkpoint, about every 60 sconsistent snapshot into collection-*.wt
→
Crashlast checkpoint plus journal replay

The other logs: PostgreSQL: WAL · MySQL · InnoDB: redo log and binary log · the pattern behind all five, and where they break

Next · chapter 3What happens on every writeCache, journal, checkpoints and index order← Chapter 1: Why MongoDB
Part 1 of 4 · Learn · 56 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect