Technical Explainers • 6 min read

Document Databases and JSON Data Modeling

A document database stores records as documents, often using JSON-like objects with nested fields and arrays. Document modeling decides which related data should be embedded inside a document and which should be stored separately and referenced.

The central design question is: What data belongs together for the way the application reads and updates it? A document can represent a useful business aggregate, but putting everything into one giant object creates other problems.

What a document looks like

A course document might contain:

{
  "course_id": "CS101",
  "title": "Introduction to Computing",
  "department": "Computer Science",
  "instructor": {
    "instructor_id": 8,
    "name": "Dr. Noor"
  },
  "tags": ["programming", "first-year"]
}

The nested instructor field and tags array are part of the same document. A document database may let the application retrieve the entire object in one query or update selected nested fields using its query language.

Embed or reference?
Embedding and referencing exchange read convenience for update and growth behavior.

Embedding versus referencing

Embedding puts related information directly inside the parent document. Referencing stores a key pointing to another document or collection.

Embedding makes sense when related data is usually read together, has manageable size, and shares a useful update boundary. Referencing is often better for relationships with independent lifetimes, very large collections, or information reused in many places.

For example, embedding an instructor’s name in every course can make course-page reads simple. But when the instructor changes their name, all duplicated copies may need updating. Storing an instructor_id reference avoids that duplication while requiring a second lookup or join-like operation.

Atomicity boundaries

Many document databases offer atomic updates to one document. Multi-document transactions may also be supported, but guarantees and costs differ across products.

Suppose a student’s profile document embeds all enrollment choices. Updating one student might be naturally atomic. Reserving the final seat in a shared course, however, affects a value that many students depend on. Document modeling does not remove the need for correct cross-document concurrency control.

Query-driven indexing

A document store may support indexes on nested paths such as instructor.instructor_id or elements of an array. Indexes still cost storage and maintenance work.

One mistake is to assume that putting data in JSON means queries are automatically fast. Without an appropriate index, searching millions of documents by a nested property may require a large scan.

Unbounded arrays are dangerous

An array of course tags might stay small. An array containing every enrollment ever made for a popular course could grow without bound. Large documents can become expensive to update, transfer, and lock; some engines also impose maximum document sizes.

Store a limited set of naturally bounded child values together, and use separate records for unbounded one-to-many relationships. The correct boundary follows access patterns and growth, not a blanket rule to embed everything.

Schema flexibility is not schema absence

Document collections often permit documents with different field sets. That can simplify gradual application evolution, but data still needs agreed meanings and validation.

An application expecting course_id to be a string can fail if an old record stores it as an integer. Use collection validators, application contracts, and versioned migrations where appropriate. “Schema-less” should not mean “no schema discipline.”

Document stores versus JSON in relational databases

PostgreSQL supports JSON and JSONB columns, along with indexing and JSON operators. A system can therefore combine relational keys and transactions with flexible document-shaped fields.

Choosing a dedicated document database is an architectural choice, not a requirement simply because an API exchanges JSON. Evaluate queries, transaction boundaries, replication, indexing, and operational needs.

A modeling checklist

For each piece of related data, ask: Is it read together? Updated together? Shared by many parents? How quickly can it grow? Does it need independent querying or retention? Can the required invariant fit within one document’s atomic update rules?

Then choose embedding, referencing, or a hybrid. Document modeling succeeds when it places common operations near the data they need without hiding expensive duplication or unbounded growth.

Work through a document-growth problem

A student document contains their profile and a small array of preferred courses. That may be reasonable. If the application later appends every login event to the same document forever, it will become large and frequently updated.

A better model is often a separate event collection keyed by student and time. The profile remains compact, while the event stream can be indexed, retained, or partitioned independently.

A reference is not always a foreign key

In many document engines, storing instructor_id: 8 does not automatically enforce that an instructor document with ID 8 exists. The application or database-specific feature may need to protect referential integrity.

If an instructor is deleted, what happens to courses that refer to them? Decide whether deletion is forbidden, the reference becomes null, or a historical instructor snapshot must remain. These are business rules, not merely document-shape choices.

Plan schema evolution

Imagine older course documents have teacher_name, but a new release expects nested instructor.name. The application needs a transition strategy: read both shapes temporarily, migrate documents, or version the schema.

Flexible documents are valuable because fields can evolve, but data contracts and migration tests are still needed. Flexibility removes some structural restrictions; it does not remove the cost of maintaining correct meaning over time.

Updating part of a document

Suppose a course document changes its title but not its instructor or tags. A document engine may offer a field-level update operation that changes just the title logically, even if its physical storage mechanism rewrites more bytes internally.

A naive application that reads the entire document, modifies it locally, and then overwrites the whole object can lose concurrent changes from another request. Use version checks, conditional updates, or the engine’s atomic field operations when that distinction matters.

This is the document-model version of a familiar relational problem: a convenient record shape does not automatically make concurrent writes safe.

Query-language portability

Document query syntax varies across products. Some support JSON-style filters, some offer SQL-like languages, and others use specialized APIs. A field path such as instructor.name may be indexable in one system but require a different index declaration in another.

Test the actual query and its plan before standardizing a large collection model. Data shape and access path must agree.

  • Relational versus NoSQL databases
  • Key-value databases and caching
  • Wide-column databases and query-driven modeling
  • Graph databases and graph queries

Related articles in

Image preview

Fit