gpt4 book ai didi

google-app-engine - 如何使用 GoLang 在 Google App Engine Datastore 中按日期/时间过滤

转载 作者:IT王子 更新时间:2023-10-29 00:44:13 25 4
gpt4 key购买 nike

我正在研究在 Google App Engine 上运行的 golang 后端。我有一个名为 Recruit 的实体有一个属性

UpdatedAt time.Time `datastore:"updated_at"`

我想按更新时间查询新兵。我的第一直觉是使用 filter .

query = datastore.NewQuery("Recruit").Filter("updated_at <=", updatedAt)

其中 updatedAt 是一个字符串。我试过表格 2014-08-28 01:53:192014-08-28T01:53:19.00618Z (前者是 time.Time 属性在数据存储中采用的形式,而后者是它在 JSON 中传播的方式。)

当我用 <= 调用查询时,返回所有 Recruit 对象。用 >= 打电话时, 返回 null。我很确定我正在测试数据集中间的时间。

当我在 console.developers.google.com 上测试时,控制台过滤器支持 a date and time after/before , 但试图替换 <=after导致错误。

有没有人设法按日期或时间过滤查询?

最佳答案

尝试使用 time.Time 类型的 updatedAt 而不是字符串

以下是我如何设法按时间过滤查询的片段:

我的属性(property):

TimeStamp time.Time

我的查询:

timeNow := time.Now()
time1hr := timeNow.Add(-59*time.Minute)
q := datastore.NewQuery("Recruit").Filter("TimeStamp <=", time1hr)

此代码会在 time1 小时之前为我提供所有实体。

关于google-app-engine - 如何使用 GoLang 在 Google App Engine Datastore 中按日期/时间过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25540427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com