الانتقال إلى المحتوى
قواعد البياناتNoSQL· 6 دقائق للقراءة

Schemaless Doesn't Mean No Schema: SQL vs NoSQL Without the Hype

A document store skips the schema check, not the schema. It moves into every piece of code that reads the data, along with every old shape.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A mobile release renamed total to totalAmount in new order documents. Nothing failed: the inserts were accepted, the API returned 200, and the monthly revenue quietly went down. In a test that stored three order documents as JSON in SQL Server 2022, standing in for a document store, the revenue query summed $.total and returned 195.50 for orders worth 294.50. There was no error; the third document was simply skipped, because nothing at write time said an order must have a total.

The common misconception

The belief: NoSQL databases are schemaless, so you can skip data modeling and never migrate data.

It's half true, which is what makes it convincing. MongoDB's docs say documents in one collection aren't required to have the same fields or data types, and that you can add new fields without rewriting existing documents or running a schema migration. There's no ALTER TABLE, no migration script in the pull request, and a prototype moves fast. Years of "rigid SQL versus flexible NoSQL" talks did the rest.

What actually happens

The schema moved into your code

Martin Fowler's slide title is blunt: "schemaless structures still have an implicit schema". His reason: "Any code that manipulates the data needs to make some assumptions about its structure, such as the name of fields." Microsoft's comparison of data store models puts it in two phrases: relational databases are schema on write, document stores are . The database accepts any shape, and every reader decides what it means. The report in the test was a reader that assumed total. Fowler's slide finishes the thought: data that doesn't fit the "will not be manipulated properly, leading to errors."

You still model, just around access patterns

Document modeling isn't optional; it's different. MongoDB calls it a core principle that data accessed together should be stored together, and advises planning the schema before production scale. Microsoft's Cosmos DB guide says data modeling in a schema-free database is "as important as ever." Embed related data when it's contained, one-to-few, read together, changes rarely and doesn't grow without bound. Reference it when it's one-to-many or many-to-many, changes often or is unbounded. Embedding a copy brings back the update problem normalization solves: the same guide accepts that if an author changes their name, every book document that embeds it needs an update. And references are weak: Cosmos DB has no foreign key constraints, so checking that a referenced document exists is up to your application or server-side code.

Migrations become lazy, not optional

Old documents keep their old shape until something rewrites them. Fowler describes the usual technique: access code reads either the old or the new format and saves only the new one, so data migrates gradually, and the old-format code is removed once the migration is done. His conclusion: "Similar migration techniques are usually required with schemaless databases too, although flexible access code does help."

Most document stores can enforce a schema

MongoDB's schema validation applies JSON Schema rules to inserts and updates, and by default rejects any write that would produce an invalid document. Its docs recommend it for an established application with a defined data structure. The flexibility runs both ways: relational databases have schemaless corners too. In the test, a CHECK constraint on the SQL Server JSON column, requiring $.total, rejected the next totalAmount document with error 547.

Write the implicit schema down

C#
using System.Text.Json.Nodes;// The implicit schema: every order has a decimal "total"static decimal Revenue(IEnumerable<JsonObject> orders) =>    orders.Sum(o => (decimal?)o["total"] ?? 0m); // v2 orders add 0

The right version fails loudly on a shape it doesn't know, instead of counting it as zero. Once the shape is stable, let the database check new writes too. This is adapted from MongoDB's validation docs and wasn't run here:

JavaScript orders-validator.js
// mongosh: check new writes; existing invalid documents can still be updateddb.runCommand({  collMod: "orders",  validator: {    $jsonSchema: {      bsonType: "object",      required: ["customer", "total"],      properties: {        total: { bsonType: ["double", "decimal"] }      }    }  },  validationLevel: "moderate"});
QuestionRelational databaseDocument store
Who checks the shapeThe database, on writeYour code, on read, unless you add validation
Changing a shapeA migration: alter and backfillReaders handle old and new until you backfill
RelationshipsJoins and enforced foreign keysEmbedding, or weak references your code checks
Default atomic unitAny rows in one transactionOne document, per Microsoft's comparison
Best fitMulti-entity transactions, ad hoc queriesAggregates read and written as a whole

How to apply it in your project

  1. Write the schema downGive each collection one owner: a data access layer with typed models, a JSON Schema file, or both. Fowler suggests a clear data access layer and/or a predicate schema to reduce the problems of a hidden, implicit one.
  2. Model from the queriesList the reads and writes each screen and job performs, then decide what to embed and what to reference with the rules above. Watch for arrays that can grow without bound.
  3. Migrate on purposeWhen a shape changes, bump schemaVersion, read both versions, write only the new one, backfill the rest, and delete the old branch. Track how many old documents remain.
  4. Choose the store per workloadMicrosoft notes that most production systems use more than one storage model, an approach called . Use a document store for aggregates that evolve, and a relational database where you need multi-entity transactions and enforced relationships.

Key takeaways

  • Schemaless means the database doesn't check the shape; the schema still exists in every reader.
  • A missing or renamed field doesn't fail on write: in the test, it silently removed 99.00 from a revenue total.
  • Document databases still need modeling, built around access patterns: embed what's read together and bounded, reference the rest.
  • Migrations don't disappear; they become gradual, and old shapes stay until you backfill them.
  • Write the schema down, version documents, and turn on validation once the shape is stable.

📚 المصادر

  1. Schemaless Data Structures — Martin Fowler
  2. Understand data models — Azure Architecture Center, Microsoft Learn
  3. Data modeling in Azure Cosmos DB — Microsoft Learn
  4. Data Modeling — MongoDB Database Manual
  5. Specify Validation Level for Existing Documents — MongoDB Database Manual

التعليقات (0)