This article mainly introduces how python converts the ObjectId in MongoDB to a timestamp. it involves Python's skills for operating MongoDB and string conversion, and has some reference value, for more information about how to convert ObjectId in MongoDB to timestamp, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
The first four digits of the _ id field in MongoDB are the hexadecimal representation of the timestamp. it is easy to extract the timestamp from _ id through Python.
Def timestamp_from_objectid (objectid): result = 0 try: result = time. mktime (objectid. generation_time.timetuple () handle T: pass return result
Call method:
Print (timestamp_from_objectid (ObjectId ('5217a543dd99a6d9e0f74702 ')))
Return value: 1377252547.0
PS: Here we recommend a Unix timestamp conversion tool for this site, with the Unix timestamp operation methods for various languages (Python/PHP/java/MySQL, etc:
Unix timestamp conversion tool:Http://tools.jb51.net/code/unixtime
I hope this article will help you with Python programming.