I am new to mongodb-go-driver and i am stuck.
I have a date inside a struct like:
type Email struct {
Date string `json:"date"`
}
the Dates on my mongoDB and mapped in my struct have the values like "02/10/2018 11:55:20".
I want to find on my DB the elements that Date are after an other date, i'm trying this but the response is always null.
initDate, _ := time.Parse("02012006", initialDate)
cursor, err := emails.Find(context.Background(), bson.NewDocument(bson.EC.SubDocumentFromElements("date", bson.EC.DateTime("$gt", initDate.Unix()))))
what am i doing wrong?
The unstable
bsonx
package inmongodb-go-driver
has aDateTime
Type. You can add the field in your struct like this:To declare the struct use
bsonx.DateTime(millis int64)
:*
time.Now().UnixNano()/1e6
basically gets the unix millis.And you can convert it to
time.Time
withemail.Date.Time()