I'm trying to query records from mongodb using golang but seems like it's not working. I tried running the query with mongo cli using ISODate() and it works but I'm not sure how to call ISODate with golang. Below is a snippet of the code I'm running.
type Record struct{
ID primitive.ObjectID `bson:"_id"`
PropertyID primitive.ObjectID `bson:"propertyID"`
CreatedAt primitive.DateTime `bson:"createdAt"`
}
// ....
cur, err := collection.Find(ctx, bson.M{"createdAt": bson.M{
"$gte": time.Now().UTC().AddDate(-1, 0, 0).Format(time.RFC3339),
}})
// ....
var recs []Record
err = cur.All(ctx, &recs)
if I run len(recs) I get 0 but in mongo-cli I'm getting result when I run code below.
db.getCollection('records').find({
createdAt: {
$gte: ISODate("2019-11-02T23:16:58+08:00")
}
}).sort({createdAt: -1})
by the way 2019-11-02T23:16:58+08:00 is the output of time.Now().UTC().AddDate(-1, 0, 0).Format(time.RFC3339)
Thanks in advance guys :D
所有评论(0)