I have a mongodb collection that I am trying to limit the result set, but the data was created by someone else and the date format appears to be stored as "2016-05-12 00:00:00.000Z". I am unsure how to format my find query to limit this type of date. I have been successful with other types of date formats, but not this one. Has anyone else encountered this issue?
myCollection {
"_id" : {
"$oid" : bson.ObjectId
},
"createdDate" : {
"$date" : Date
}
This is what I have attempted.
toDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
fromDate := toDate.AddDate(0, 0, -1)
collectionResult := collectionRecords.Find(bson.M{"retailer": result.Id, "createdAt": bson.M{"$gte": fromDate}})
Ultimately, I would like to limit to one day and was attempting to do the following:
collectionResult := collectionRecords.Find(bson.M{"retailer": result.Id, "createdAt": bson.M{"$gte": fromDate, "$lt": toDate}})
collectionResult.All(&collectionData)
collectionCount, _ := collectionResult.Count()
fmt.Println("Count: ", collectionCount)
it always returns 0. Like I stated above I have been successful with UTC dates, but this one seems to trouble me.
If I understand the question, you're looking how to create a UTC timestamp 24 hours into the past where hours have been truncated?
You can try filtering on these dates:
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now().Truncate(24*time.Hour).UTC()
yesterday := today.Add(-24*time.Hour)
fmt.Println(today)
fmt.Println(yesterday)
}
The code in the above post actually works for the "Zulu" date time stamp. This is a non-issue.