Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

818
Views
How to insert datetime string into Mongodb as ISODate using pymongo

How to insert datetime string like this "2017-10-13T10:53:53.000Z" into mongo db as ISODate? I get a string in mongodb when I insert: datetime.strptime("2017-10-13T10:53:53.000Z", "%Y-%m-%dT%H:%M:%S.000Z")

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This works for me:

from pymongo.mongo_client import MongoClient
import datetime

d = datetime.datetime.strptime("2017-10-13T10:53:53.000Z", "%Y-%m-%dT%H:%M:%S.000Z")

with MongoClient() as mongo:
    db = mongo.get_database("test")
    db['dates'].insert({"date" : d})

Check in mongo:

> use test
switched to db test
> db.dates.findOne()
{
    "_id" : ObjectId("589307d7cfd6c908d4b677d6"),
    "date" : ISODate("2017-10-13T10:53:53Z")
}

UPDATE: As commented, if you get a "time data does not match format" error, try a more general format string such as: %Y-%m-%dT%H:%M:%S.%fZ

over 4 years ago · Santiago Trujillo Report

0

use dateutil dateutil.parser.parse("2017-10-13T10:53:53.000Z") will return datetime.datetime(2017, 10, 13, 10, 53, 53, tzinfo=tzutc())

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!