Python converts ObjectId in MongoDB to timestamp-Python tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.