Lesson 2 of 14

The real cost of null vs missing fields - 005

null store cheyyadama leka field eh remove cheyyadama? Chinna logic eh kani, Sparse indexes vadetappudu shock istundi.

Core Explanation: null is a value. Missing field is... nothing. null store chesthe BSON space tintundi, plus indexing lo entry padutundi. Missing field aithe, Sparse Index use chesinappudu index lo entry undadu, so index size taggutundi.

Wrong Practice: Storing explicit nulls unnecessarily.

{ "cancelReason": null }
{ "cancelReason": null }
// Index on 'cancelReason' will be huge and full of nulls.

Best Practice: Omit the field entirely if not applicable.

// Document 1 (Active)
{ "status": "active" } 
// Document 2 (Cancelled)
{ "status": "cancelled", "cancelReason": "reason..." }

// Create Sparse Index
db.orders.createIndex({ "cancelReason": 1 }, { sparse: true })

Closing Insight: "Optional fields ni null set cheyakandi, just field ne lepeyandi. Sparse indexes happy ga feel avtay."