时间GT查询不适用于Mongoid和Ruby在Rails上 [英] Time gt query not working with mongoid and ruby on rails

查看:64
本文介绍了时间GT查询不适用于Mongoid和Ruby在Rails上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在铁轨上使用蒙古包和红宝石.

I am using mongoid and ruby on rails.

我正在使用蒙古语存储created_at时间

I am storing created_at time with mongoid using

include Mongoid::Timestamps

所以说我有3条这样的帖子

So let's say I have 3 post created like this

{"post":"1","created_at": "2014-03-25 13:04:43"}
{"post":"2","created_at": "2014-03-25 13:04:44"}
{"post":"3","created_at": "2014-03-25 13:04:45"}

现在我想获取所有在{"post":"2"}之后创建的帖子,也就是在"2014-03-25 13:04:44"之后创建的帖子,因此应该只返回{"post":"3"}

Now I want to get all posts which is created after {"post":"2"}, that is after "2014-03-25 13:04:44", so that should only return {"post":"3"}

Model.where(:created_at.gt => "2014-03-25 13:04:44")

但是通过上面的查询,它返回了{"post":"2"}{"post":"3"},因此{"post":"2"}应该不存在.

But with above query it is returning {"post":"2"} and {"post":"3"}, so {"post":"2"}, should not be there.

.lt查询也可以正常工作.

Also .lt queries work as expected.

知道为什么会这样吗?

谢谢.

推荐答案

问题出在时区...蒙古语created_at字段存储在UTC时区

the problem is in time zone... the mongoid created_at field is stored in UTC time zone

您可以使用以下有效的方法

you can use the following which will work

Model.where(:created_at.gt => post_2_instance.created_at)

假设post_2_instance{"post":"2","created_at": "2014-03-25 13:04:44"}

但是如果您想使用日期查询,则可以使用以下内容

but if you want to query using date you can use the following

Model.where(:created_at.gt => DateTime.parse("2014-03-25 13:04:43").in_time_zone('UTC'))

怪异的行为报告给此JIRA问题

这篇关于时间GT查询不适用于Mongoid和Ruby在Rails上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆