NoSQL and Document Databases

Definition

NoSQL document databases (e.g. MongoDB) store rich, self-describing documents instead of rows in fixed tables. There is no declared schema, no join and no cross-document constraints — related data is either embedded (pre-joined into one document) or joined in the application. Operations on a single document are atomic.


Core Ideas

Schema design by embedding

Favor rich documents that pre-join the data a query needs. Benefits:

  • Latency — get the first bit of data in one read
  • Bandwidth — fetch related bits without extra round-trips

The cost is duplication and application-enforced integrity (no referential integrity, no declared schema).

Aggregation pipeline

Documents flow through ordered stages, each transforming the stream:

StagePurposeRatio
$projectreshape1:1
$matchfiltern:1
$groupaggregaten:1
$unwindnormalize an array1:n
$sort / $skip / $limitordering & paging
$outwrite results to a collection1:1

Group accumulators include $sum, $avg, $min, $max, $push, $first/$last (sort first). $sort is bounded to 100 MB unless allowDiskUse is set.

Replication

  • A replica set provides redundancy and failover via an oplog in the local db that secondaries tail to stay in sync.
  • Members: regular, arbiter (voting only), delayed/hidden (vote but can’t become primary).
  • Write concern (w, j, wtimeout) trades durability for latency; reading from the primary gives read-your-writes consistency, reading from a secondary is only eventually consistent.
  • Make non-idempotent updates ($inc, $push) safe to retry by making them idempotent.

Sharding

Horizontal scale across many mongod via mongos. The shard key must be present in every document, is immutable, needs an index, and should be selective — a query without the shard key becomes an expensive scatter-gather.


Relationships


References

  • MongoDB Schema Design
  • MongoDB Aggregation Framework
  • MongoDB Application Engineering