
Schemaless Doesn't Mean No Schema: SQL vs NoSQL Without the Hype
- كاتب موثّق
محتويات المقال
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
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
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
Migrations become lazy, not optional
Most document stores can enforce a schema
CHECK constraint on the SQL Server JSON column, requiring $.total, rejected the next totalAmount document with error 547.
Write the implicit schema down
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 0using System.Text.Json.Nodes;// One place that knows every shape an order has ever hadstatic decimal Total(JsonObject order) => (int?)order["schemaVersion"] switch{ null or 1 => (decimal)order["total"]!, 2 => (decimal)order["totalAmount"]!, var v => throw new NotSupportedException($"Unknown order schema v{v}")};static decimal Revenue(IEnumerable<JsonObject> orders) => orders.Sum(Total);// 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"});How to apply it in your project
Write the schema down Give 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. Model from the queries List 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. Migrate on purpose When 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.Choose the store per workload Microsoft 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.
التعليقات (0)