β Scenario 5: Applications Requiring Strict Schema Enforcement
Example: Medical records, government systems, financial records
The Problem:
// SQL: Schema enforced at database level
CREATE TABLE patients (
id SERIAL PRIMARY KEY,
ssn VARCHAR(11) NOT NULL UNIQUE,
dob DATE NOT NULL,
blood_type ENUM('A+','A-','B+','B-','AB+','AB-','O+','O-') NOT NULL
);
// DB rejects invalid data automatically
// MongoDB: Schema validation is opt-in and limited
db.createCollection("patients", {
validator: {
$jsonSchema: {
required: ["ssn", "dob", "blood_type"],
properties: {
blood_type: { enum: ["A+","A-","B+","B-","AB+","AB-","O+","O-"] }
}
}
}
})
// But easier to bypass, less rigid enforcement
Issues with MongoDB's flexible schema:
- Schema validation is optional (can be disabled)
- No foreign key constraints
- Easier to have inconsistent data across documents
- Regulatory/compliance systems need guaranteed data integrity
- Harder to enforce business rules at DB level
β
Better Choice: PostgreSQL, Oracle, SQL Server
Relational databases enforce schema strictly with CHECK constraints, foreign keys, triggers, and stored procedures.