Skip to content
C

Document Model


Document Model

Overview

The document model stores data as self-contained, semi-structured documents — typically JSON or BSON (binary JSON) — where each document can have nested objects and arrays, and different documents in the same collection are not required to share an identical schema (schema-on-read rather than schema-on-write). This is a member of the broader NoSQL family, popularized by MongoDB and CouchDB.

How it works — a worked example

A user profile in a document database might look like:

json
{ "_id": "u123", "name": "Priya", "email": "priya@example.com", "addresses": [ { "type": "home", "city": "Pune", "zip": "411001" }, { "type": "work", "city": "Mumbai", "zip": "400001" } ], "preferences": { "newsletter": true, "theme": "dark" } }

Everything about this user — including multiple addresses and nested preferences — lives in one document, retrievable in a single read with no joins. A different document in the same users collection could omit preferences entirely or add a new field like loyaltyTier, and that's perfectly valid — the collection has no rigid, enforced schema the way a relational table does.

Edge cases and trade-offs

  • Denormalization is the norm, not the exception: data that would be normalized into separate relational tables (addresses, preferences) is embedded directly, which is fast for reads but means updating a piece of data duplicated across many documents requires updating every copy.
  • Rich internal query support: unlike key-value stores, document databases can query into the nested structure — e.g., "find all users whose addresses.city is 'Pune'" — because the database understands the document's internal fields and can index them.
  • Multi-document transactions exist but are costlier: MongoDB added ACID multi-document transactions in version 4.0+, but the model's natural strength is single-document atomicity, not cross-document joins/transactions.
  • Schema flexibility is a double-edged sword: it speeds up iteration (no migrations to add a field) but pushes data-integrity responsibility onto the application layer — there's no built-in foreign key enforcement between collections the way relational databases enforce it between tables.

Differentiating from Key-Value (3.7) and Column-Family (3.8) models

This is the most commonly confused trio in NoSQL, so be precise:

  • Document vs. Key-Value: a key-value store treats its value as an opaque blob — the database cannot see or query fields inside it, only fetch/store by key. A document store understands the internal structure of the document and can query, index, and filter on nested fields (like addresses.city above). Every document store is technically retrievable by a key (_id), but the querying power inside the value is what sets it apart.
  • Document vs. Column-Family: a column-family store (Cassandra, HBase) organizes data into rows identified by a key, where each row can have a different sparse set of columns grouped into families — optimized for very high write throughput and horizontal scale across huge clusters, with a data model still structured around static column families rather than freeform nested JSON. Document stores optimize for flexible, deeply nested, application-object-shaped data, not primarily raw write-throughput at Cassandra's scale.

Key takeaways / interview Q&A

Q: Why is "schema-on-read" a defining trait of document databases? A: Because the database doesn't enforce a fixed structure at write time (schema-on-write, as relational tables do) — each document can have its own shape, and the application interprets/validates structure when it reads the data.

Q: What's the single clearest test to distinguish a document database from a key-value store? A: Can the database query and index fields inside the stored value? If yes (e.g., filter by a nested addresses.city), it's a document store; if the value is opaque and only retrievable whole by its key, it's a key-value store.

Mock Test

  • Document Model - Quick Test

    8 questions on Document Model.

    8 questions · 8 min · Medium
    Start Mock Test