MongoDb - MongoDb of _id generation rules

MongoDB's document must have a _id key.

The purpose is to confirm each document in the collection of can be uniquely identified.

ObjectId is the default type of _id.

ObjectId using 12 bytes of storage space, each two-byte hexadecimal number, is a 24-bit string.

12 generation rules:

[0,1,2,3] [4,5,6] [7,8] [9,10,11]

Timestamp | Encoding | PID | counter

  • The first four byte time stamp , may provide a unique level of seconds.

  • The next three bytes of the host that is a unique identifier, typically a hash value of the hostname of the machine.

  • The next two bytes are generated PID ObjectId ensure ObjectId on the same machine concurrently produced is unique.

When first nine bytes to ensure the same second ObjectId different processes on different machines produced unique.

  • The last three bytes are incrementing counter, to ensure that the same process to generate the same ObjectId second is unique.

 

Thus can be obtained, when the data of the database to sort, can be ordered directly _id more (because the previous generation rule is a four-byte timestamp, a unique class of seconds)

 

Guess you like

Origin www.cnblogs.com/jianxian/p/12313079.html