ObjectId(“507f1f77bcf86cd799439011”)
ObjectId is a hexidecimal generated by MongoDB for ids. It’s an object with the methods, including getTimestamp() (get’s the creation time as a Date object. Every document in mongo must have a _id, created with the ObjectId or manually, and is automatically indexed. Either way, ids always need to be unique.*
Fun facts of ObjectIds:
- 12 bytes in size and the type is BSON
- Include the timestamp and automatically indexed - so when you sort it’s roughly in chronological order
- If you don’t set an id, mongo will generate it as an ObjectId for you - so kind of her.
*I have run into an instance working with old production code where someone thought it wouldn’t hurt anything to make the ids for a small, tiny, itsy-bity object all zeros. The frontend did not laugh at that joke when features started to break and break for no obvious reason.