Expert rules for schema design, indexing, and performance in MongoDB (NoSQL).
Schema Design:
DailyLog).Indexing:
(status, date, price) if you query status='A', sort by date, filter price > 10.$text search instead of $regex for keywords. $regex is slow (linear scan) unless anchored ().^prefixPROJECTION is key).nReturned / keysExamined ratio of ~1. If docsExamined >> nReturned, index is inefficient.Sharding (Horizontal Scale):
Timestamp, ObjectId) for high-write workloads (creates "Hot Shards"). Use Hashed Sharding or high-cardinality natural keys.Performance:
skip(): Use _id or sort-key based pagination (bucket_id > last_id). skip(10000) scans 10000 docs.$match, $group) over bringing data to client (JS).Operations:
w:1 (Ack) vs w:majority (Safe).$push without limit).