Lesson 1 of 14
ObjectId decoded: timestamp, ordering guarantees - 002
Default _id 24 characters random string anukunte.. meeru chala logic miss avthunnaru. Sorting ki timestamp field vade avasaram ledu!.
Core Explanation: ObjectId lo first 4 bytes timestamp. So, by default, _id tho sort chesthe, creation time prakaram sort avtundi. Separate ga createdAt index maintain cheyalsina avasaram max undadu, unless precise MS-level accuracy kavali ante thappa. Idi Client-side generate aithe better, server round-trip save avtundi.
Wrong Practice: Ignoring _id and creating extra indexes for sort.
// Waste of index resource
db.users.createIndex({ createdAt: 1 })
// Sort query
db.users.find().sort({ createdAt: 1 })
Best Practice: Leverage the natural sort order of ObjectId.
// Efficient
db.users.find().sort({ _id: 1 }) // Uses the default unique index
Closing Insight: "Pagination ki _id ni vadandi, skip() and limit() kante chala fast ga untundi."