Python converts the ObjectId in MongoDB to a timestamp method
- 2020-04-02 14:39:22
- OfStack
This article illustrates how python converts ObjectId in MongoDB to a timestamp. Share with you for your reference. Specific analysis is as follows:
The first four bits of the _id field in MongoDB are hexadecimal representations of the timestamp, which can be easily extracted from _id through Python
def timestamp_from_objectid(objectid):
result = 0
try:
result = time.mktime(objectid.generation_time.timetuple())
except:
pass
return result
Call method:
print(timestamp_from_objectid(ObjectId('5217a543dd99a6d9e0f74702')))
Returns: 1377252547.0
PS: here again for you to recommend a site Unix timestamp conversion tool, with a variety of languages (Python/PHP/ Java /MySQL, etc.) Unix timestamp operation method:
Unix timestamp conversion tool: (link: http://tools.jb51.net/code/unixtime)
I hope this article has helped you with your Python programming.