Create Between Query using gorethink

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

Create Between Query using gorethink

问题

如何将这个rethinkdb查询转换为gorethink查询:

r.DB("arkinventory").Table("reportsdata").Between(r.Time(2012, 8, 13, 23, 32, 49, 923000000, "Z"), r.Time(2013, 8, 13, 23, 32, 49, 923000000, "Z"), r.BetweenOpts{Index: "updated_at"}).Run(session)

我尝试了以下代码:

r.DB("arkinventory").Table("reportsdata").Filter(func(row r.Term) r.Term {
    return row.Between(r.Time(2014, 8, 12, 0, 0, 0, "Z"), r.Time(2014, 8, 12, 0, 0, 0, "Z"), r.BetweenOpts{Index: "updated_at"})
}).Run(session)

你可以尝试使用 row.Daterow.ToEpochTime 来获取结果。如果还无法得到结果,请帮助我完善这个gorethink查询。

英文:

How to convert this rethinkdb query into gorethink query

r.db("arkinventory").table("reportsdata").between(new Date("2012-08-13T23:32:49.923Z"), new Date("2013-08-13T23:32:49.923Z"), {index: "updated_at"})

I tried

.Filter(func(row r.Term) r.Term {
		return row.Between(r.Time(2014, 8, 12, 'Z'), r.Time(2014, 8, 12, 'Z'), r.BetweenOpts{Index: "updated_at"})
	}).Run(session)

row.Date
row.ToEpochTime

But yet not able to get the result. Please help me formulate this query in gorethink.

答案1

得分: 1

t := r.Table("yourTable")
t.Between(from, to,
r.BetweenOpts{Index: "updated_at"}
).OrderBy(r.OrderByOpts{Index: r.Desc("updated_at")}).Run(dbs)

这里的fromto取决于你保存这些值的方式,例如int64、string、time等。根据之前的示例,r.Time(args)应该可以工作。

如果索引不存在,可以通过以下方式创建索引:

t.IndexCreate("updated_at").Exec(dbs)
t.IndexWait().RunWrite(dbs)

英文:
t := r.Table("yourTable")
t.Between(from, to,
    r.BetweenOpts{Index: "updated_at"}
).OrderBy(r.OrderByOpts{Index: r.Desc("updated_at")}).Run(dbs)

Here, from and to depends how you save these values i.e int64, string, time, etc. According to previous example r.Time(args) should work.

If nonexist, create index via:

t.IndexCreate("updated_at").Exec(dbs)
t.IndexWait().RunWrite(dbs)

答案2

得分: 0

根据dancannon在gitter频道上的回复,我们应该在日期上使用During而不是Between。

Filter(func(row r.Term) r.Term {
    return row.Field("updated_at").During(r.Time(2014, 8, 12, 'Z'), r.Time(2014, 8, 12, 'Z'))
}).Run(session)
英文:

As per dancannon's response on gitter channel https://gitter.im/dancannon/gorethink

We should use During instead of Between for Dates.

Filter(func(row r.Term) r.Term {
    return row.Field("updated_at").During(r.Time(2014, 8, 12, 'Z'), r.Time(2014, 8, 12, 'Z'))
}).Run(session)

huangapple
  • 本文由 发表于 2015年7月10日 15:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/31335258.html
匿名

发表评论

匿名网友

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

确定