Nanoseconds lost coming from MongoDB ISODate Object Nanoseconds lost coming from MongoDB ISODate Object mongodb mongodb

Nanoseconds lost coming from MongoDB ISODate Object


MongoDB stores documents in BSON format and its specification says:

BSON Date is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970).

So, date precision is limited to miliseconds. If you really need to store nanoseconds, you shouldn't use date type. You have to use a long and store the timestamp, so that you'll no lose precision.


This is a bug in the way that the MongoDB Perl driver interacts with DateTime. When ISODate values are retrieved from the database, the driver initializes the DateTime object using its from_epoch constructor. But the nanosecond portion is not passed to the constructor.

DateTime does support passing the full value including nanoseconds, and the driver should be updated to fix that.

I've created a ticket for this bug and I will fix it. But maybe not until after the holiday weekend. :)