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

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
Eoin