I am relatively new to Golang and MongoDB and ran into a date issue where it appears that I can insert a UTC date into MongoDB, but when I query through Golang it is getting automatically converted to the local time. I want to get it back from MongoDB in UTC with no conversion. Here is a quick example:
type SampleItem struct {
ObjId bson.ObjectId `bson:"_id,omitempty" json:"-"`
SampleDate time.Time `bson:"sampleDate" json:"sampleDate"`
}
func TestMe() {
var item SampleItem
var items []SampleItem
sess := getSession()
defer sess.Close()
item.SampleDate = time.Now().UTC()
fmt.Printf("%s
", item.SampleDate)
collection := sess.DB("myCollection").C("sampleItems")
collection.Insert(item)
err := collection.Find(bson.M{}).All(&items)
if err == nil {
fmt.Printf("%s
", items[0].SampleDate)
}
}
My output:
2014-10-12 04:10:50.3992076 +0000 UTC
2014-10-11 23:10:50.399 -0500 CDT
It appears that the mgo driver may be automatically converting it because when I query mongodb from a console window my date is in UTC. Am I missing a mgo option somewhere that turns this off?