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:
| Stage | Purpose | Ratio |
|---|---|---|
$project | reshape | 1:1 |
$match | filter | n:1 |
$group | aggregate | n:1 |
$unwind | normalize an array | 1:n |
$sort / $skip / $limit | ordering & paging | — |
$out | write results to a collection | 1: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
oplogin thelocaldb 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
- Databases — parent domain; contrast with relational modeling
- Query Optimization — indexing and covered queries apply to MongoDB too
- Distributed Consensus — replica-set elections are a consensus problem
- System Design — sharding and replication are scalability primitives
References
- MongoDB Schema Design
- MongoDB Aggregation Framework
- MongoDB Application Engineering