How to insert an ISODate entry from Python with Pydantic and Motor

In the movies collection we can see two different types of date type. The releaased key has ISODate datatype, but the lastupdated key has a string datatype.
I can convert a date string to a datetime type and send it to MongoDB. If I retrieve the data, then I’ll get a string as the lastupdated value.
I can write new Date("2017-04-10") in the Mongo Shell.
I created a pydantic model and use motor as the mongoDB driver in Python.
However, sending a datetime dict entry to mongoDB denerate a date string instead of ISODate. How can I create a new date entry with the ISODate type in Python with motor driver?

Suppose I have a birthday field in my document (time is irrelevant). What is the best way to define the datatype, a date string data type or ISODate?

Hi @ywiyogo

I think this is a broader question outside of M220P and you might find it useful to post in the Drivers & ODMs forum around motor as M220P only used PyMongo nor does the course use pydantic or any type hinting library.

In terms of MongoDB, BSON has a specific Date type (see this docs page). It is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970). In PyMongo datetime.datetime objects are used to store dates and times in MongoDB documents (see this docs page). Essentially, any driver whether PyMongo or indeed motor maps a data back to the underlying BSON.

In Python’s case with MongoDB, the recommended format is to use datetime.datetime object to store a time field such as your birthday field.

Hopefully this helps answer your question and for a wider audience, I’d refer you to the Drivers and ODMs forum.

Kindest regards

Thanks Eoin for the hint. I’ve just moved this thread to the “Drivers and ODMs”.

The issue I observed was that if I insert a document with a date entry in Mongo Shell, it creates ISODate. If I use Pydantic and Motor with datetime.datetime, it creates date string. This is the output of my example:

{ "_id" : "60a18c13732f278885f81a03", "name" : "Joe Doel", "email" : "", "birthday" : ISODate("2000-06-04T00:00:00Z"), "location" : "Berlin" }
{ "_id" : "60a18c2d732f278885f81a04", "name" : "Alice Will", "email" : "", "birthday" : "1996-02-01T00:00:00", "location" : "Amsterdam" }

How can I achieve a consistency in my date type for Mongo Shell and Python?
I’ll try using PyMongo with datetime if it also creates ISODate or not.

Hi @ywiyogo

So essentially the MongoShell itself is a custom driver and it wraps a Date object with the ISODate for convenience (see this page for more details). In terms of query in Python for date/times, you should use datetime, the representations between the two mechanisms to query the data means it’s not possible to easily find consistency and instead you should use the appropriate date representation for the MongoShell or for the Python Driver.

Hopefully, this helps to clarify your question, and good luck with what you are building!

Kindest regards,

So, it’s not possible to have consistency between both drivers.
Thanks for the clarification @Eoin_Brazil!

Hi @ywiyogo

Not in the visual presentation sense, in the underlying data since both are consistent to the same BSON date type but that’s not rendered in the same consistent fashion and I think that was your question.

Glad this helped!

Motor (and PyMongo) insert datetime objects as BSON date type in the same way that the mongo shell inserts ISODate objects. It sounds like Pydantic is automatically converting Python datetime objects into strings before sending the document to Motor.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.