Lesson 5 of 14
Why MongoDB is not “schema-less” - 003
MongoDB schema-less kada, emaina toseyochu anukunte production lo 'data rot' confirm. Schema-less kadu, dynamic schema adi!
Core Explanation: Schema code lo lekapoyina, data lo undi. Database enforce cheyyadu ante, mee application enforce cheyyali ani artham. Okavela field names marithe, leda types marithe, app code lo 100 if-else conditions rayalsi vastundi. Schema validation rules database level lo pettadam mandatory for sanity using $jsonSchema.
Wrong Practice: Mixing data types blindly.
// Chaos in one collection
{ "price": "100" } // String
{ "price": 100 } // Number
// Queries will behave unexpectedly!
Best Practice: Enforce Schema Validation.
db.createCollection("products", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["price"],
properties: {
price: { bsonType: "int" }
}
}
}
})
Closing Insight: "Freedom unna chota discipline undali. Validation rules set cheyyandi, lekapothe future lo edustaru."