On the ObjectId

ObjectId

MongoDB each set of each document storage must have a "_id" key, the default is ObjectId object.

"_Id" unique identification as the current document in the collection.

71standby:PRIMARY> db.accesslog.find({},{_id:1}).pretty()
{ "_id" : ObjectId("5d1d7225d06339b452d964f6") }
{ "_id" : ObjectId("5d1d7225d06339b452d964f7") }
{ "_id" : ObjectId("5d1d7225d06339b452d964f8") }
{ "_id" : ObjectId("5d1d7226d06339b452d964fa") }
{ "_id" : ObjectId("5d1d7226d06339b452d964fb") }
{ "_id" : ObjectId("5d1d7226d06339b452d964fc") }
{ "_id" : ObjectId("5d1d72acd06339b452d964fe") }
{ "_id" : ObjectId("5d1d72acd06339b452d964ff") }
{ "_id" : ObjectId("5d1d72acd06339b452d96500") }
71standby:PRIMARY> 

ObjectId is 12 bytes, which is the first four bytes is the time stamp of document creation (eight hours later than Beijing)

In the mongo shell where you can pass ObjectId.getTimestamp() 来查看文档创建时间。

71standby:PRIMARY> x = ObjectId()
ObjectId("5d9ea85b7444a82a869f958f")
71standby:PRIMARY> x
ObjectId("5d9ea85b7444a82a869f958f")
71standby:PRIMARY> x.getTimestamp()
ISODate("2019-10-10T03:41:15Z")
tvos-mediacenter-rs1:PRIMARY> 

 

Time Conversion

To "5d9ea85b7444a82a869f958f" as an example:

Byte # 1 is equal to 8bit, hexadecimal number and may be represented by 4bit, so the two may represent a hexadecimal byte

# So the first 4 bytes are the first eight hexadecimal digits.

In [25]: import time                                                                                                                                                                                                                      

In [26]: key = '5d9ea85b7444a82a869f958f'                                                                                                                                                                                                 

In [27]: ts = int (key [: 8], 16) # 16 hex 10 hex rpm                                                                                                                                                                                                      

In [28]: ts                                                                                                                                                                                                                               
Out[28]: 1570678875

In [29]: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))                                                                                                                                                                           
Out [29]: '2019-10-10 11:41:15' # UTC time earlier than 8 hours

In [30]: hex (ts) # 10 hex 16 hex rpm                                                                                                                                                                                                      
Out[30]: '0x5d9ea85b'

In [31]: 

In [32]: 11*16**0 + 5*16**1 + 8*16**2 + 10*16**3 + 14*16**4 + 9*16**5 + 13*16**6 + 5*16**7 
Out[32]: 1570678875

In [33]:

  

  

Guess you like

Origin www.cnblogs.com/standby/p/11647158.html