Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert mongodb go driver's primitive.Timestamp type back to Golang time.Time type?

Tags:

mongodb

go

mongo's go driver returns a bson timestamp as https://godoc.org/go.mongodb.org/mongo-driver/bson/primitive#Timestamp is there a way to convert it to time.Time

like image 418
Palash Nigam Avatar asked Aug 31 '25 21:08

Palash Nigam


1 Answers

https://docs.mongodb.com/manual/reference/bson-types/#timestamps

As you can see in mongodb official website, BSON Timestamps contains two values, 'T' for the seconds since Unix epoch and 'I' for an incrementing ordinal for operations within a given second.

So if you want to convert the bson timestamp to time.Time, you may just use time.Unix(timestamp.T, 0)

like image 128
ernesttcwong Avatar answered Sep 04 '25 00:09

ernesttcwong