NotesSystem

MongoDB

Also: Mongo · MongoDB Atlas

The most widely used document database: collections of BSON documents, each read and written as a unit. Its defining choice is to make the self-contained document, not the row, the unit of storage, indexing and atomicity.

Also in: Data Models & Query Languages › In the real world

Design choices

  • Document Model: BSON documents up to 16 MiB and 100 levels of nesting; single-document writes are atomic.
  • Schema-on-Read: collections accept any shape by default; $jsonSchema validators (since 3.6, 2017) can enforce one, as error or warning.
  • Joins, added later: the $lookup aggregation stage (3.2, 2015) is a left outer join; MongoDB's own guidance still says to design so you rarely need it.
  • Transactions: multi-document ACID transactions since 4.0 (2018) on replica sets, and since 4.2 across shards.
  • Declarative Query Language: a JSON query language plus the aggregation pipeline, a list of stages ($match, $group, $lookup) the server may reorder.
  • Leader-Based Replication: replica sets with one primary and elected secondaries; sharding by hashed or ranged shard keys.
  • Storage: WiredTiger, a B-tree engine with compression, is the default.

Notable trade-offs

  • Data that is shared or many-to-many has to be copied (and kept in sync) or referenced and joined, which the model handles less well than a relational database.
  • Unbounded embedded arrays are its signature anti-pattern: documents grow until reads slow and writes hit the size limit.

Used by

  • The Guardian ran its Composer CMS on MongoDB until 2018, then moved it to Postgres jsonb, keeping the document shape.