Reputation: 193
I'm working on a golang backend run on Google App Engine. I have an Entity called Recruit
that has a property
UpdatedAt time.Time `datastore:"updated_at"`
I would like to query Recruits by their updated time. My first instinct is to use a filter.
query = datastore.NewQuery("Recruit").Filter("updated_at <=", updatedAt)
where updatedAt is a string. I've tried the forms 2014-08-28 01:53:19
and 2014-08-28T01:53:19.00618Z
(The former is the form the time.Time property takes in the datastore, while the latter is how it propagates in JSON.)
When I call the query with <=
, all Recruit objects are returned. When calling with >=
, null is returned. I'm quite sure I'm testing with a time in the middle of the data set.
When I test on console.developers.google.com, the console filters support a date and time
after/before
, but trying to replace <=
with after
results in an error.
Has anyone managed to filter queries by date or time?
Upvotes: 4
Views: 5579
Reputation: 759
Try to use updatedAt of type time.Time instead of as a string
Following is a snippet of how I managed to filter queries by time:
My Property:
TimeStamp time.Time
My Query:
timeNow := time.Now()
time1hr := timeNow.Add(-59*time.Minute)
q := datastore.NewQuery("Recruit").Filter("TimeStamp <=", time1hr)
This code would give me all the entities before time1hr.
Upvotes: 4