mongo sets the value of id property long:
12 bytes:
- 4 bytes: timestamp →
- this is the reason we don’t have to add created at attribute, because it is already there in id
- To sort by time , we can just sort them using the id
- 3 bytes: machine identifier
- 2 bytes: process identifier
- 3 bytes: counter
- If everything is save, multiple document with same, timestamp, same machine, and same process , the counter will make them different
1 byte = 8 bits
2 ^ 8 = 256
2 ^ 24 = 16M
This id is generated by MongoDB driver
Driver → mongo db
mongoose is an abstraction over mongo db driver
To get time stamp from id :
id.getTimeStamp();
Validate id:
const isValid = mongoose.Types.ObjectId.isValid(id);