如何在使用GoLang的Google App Engine Datastore中按日期/时间进行筛选

huangapple go评论83阅读模式
英文:

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:192014-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:&quot;updated_at&quot;`

I would like to query Recruits by their updated time. My first instinct is to use a filter.

query = datastore.NewQuery(&quot;Recruit&quot;).Filter(&quot;updated_at &lt;=&quot;, 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 &lt;=, all Recruit objects are returned. When calling with &gt;=, 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 &lt;= 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(&quot;Recruit&quot;).Filter(&quot;TimeStamp &lt;=&quot;, time1hr)

This code would give me all the entities before time1hr.

huangapple
  • 本文由 发表于 2014年8月28日 11:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/25540427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定