mongodb – Proper way to store a timestamp as bson data-ThrowExceptions

Exception or error:

We’re using the Go implementation of Open API and we noticed that when storing strfmt.DateTime values in our Mongo database, the value gets stored as 0001-01-01T00:00:00.000Z, similar to this issue.

The project recently migrated to the official Mongodb driver, which seems to have caused this bug. An example of how the DateTime structs are serialized can be seen here:

func (t DateTime) MarshalBSON() ([]byte, error) {
   return bson.Marshal(bson.M{"data": t.String()})
}

The culprit seems to be that storing timestamps as strings in the bson data does not work with the new driver. What is the right way to do this?

This is not a debug request. I am wondering what the right way to store timestamps in bson format in a Mongodb database using the official Mongodb driver for Go.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *