I know how the _id column contains a representation of timestamp when the document has been inserted into the collection. here is an online utility to convert it to timestamp: http://steveridout.github.io/mongo-object-time/
What I'm wondering is if the object id string itself is guaranteed maintain the ascending order or not? i.e. does this comparison always return true?
"newest object id" > "second newest object id"
答案 0 :(得分:19)
No, there is no guarantee whatsoever. From the official documentation:
The relationship between the order of ObjectId values and generation time is not strict within a single second. If multiple systems, or multiple processes or threads on a single system generate values, within a single second; ObjectId values do not represent a strict insertion order. Clock skew between clients can also result in non-strict ordering even for values, because client drivers generate ObjectId values, not the mongod process.
答案 1 :(得分:4)
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)
This is the id structure. So only last 3 bytes will increment uniquely. So the answer of your question is yes.
答案 2 :(得分:1)
对于mongo version >= 3.4
,Objectid的生成略有变化。
其结构为:
所以前4个字节仍然是自Unix时代以来的秒数,它仍然几乎是递增的,但并不严格。
https://docs.mongodb.com/manual/reference/bson-types/#objectid