英文:
How to filter by date/time in Google App Engine Datastore using GoLang
问题
我正在使用在Google App Engine上运行的golang后端。我有一个名为Recruit
的实体,其中有一个属性:
UpdatedAt time.Time `datastore:"updated_at"`
我想通过它们的更新时间查询Recruits。我的第一直觉是使用过滤器。
query = datastore.NewQuery("Recruit").Filter("updated_at <=", updatedAt)
其中updatedAt是一个字符串。我尝试了2014-08-28 01:53:19
和2014-08-28T01:53:19.00618Z
这两种形式(前者是time.Time属性在数据存储中的形式,而后者是它在JSON中传播的方式)。
当我使用<=
调用查询时,返回了所有的Recruit对象。当使用>=
调用时,返回了空值。我非常确定我正在测试数据集中间的时间。
当我在console.developers.google.com上测试时,控制台过滤器支持日期和时间
的after/before
,但尝试用after
替换<=
会导致错误。
有人成功地通过日期或时间过滤查询吗?
英文:
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?
答案1
得分: 4
尝试使用类型为time.Time的updatedAt,而不是字符串。
以下是我如何通过时间来筛选查询的代码片段:
我的属性:
TimeStamp time.Time
我的查询:
timeNow := time.Now()
time1hr := timeNow.Add(-59*time.Minute)
q := datastore.NewQuery("Recruit").Filter("TimeStamp <=", time1hr)
这段代码将给我返回所有在time1hr之前的实体。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论