我在轨道上使用mongoid和ruby。
我使用
存储mongoid的created_at
时间
include Mongoid::Timestamps
所以,让我说我有3个这样的帖子
{"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"}
Model.where(:created_at.gt => "2014-03-25 13:04:44")
但是上述查询会返回{"post":"2"}
和{"post":"3"}
,因此{"post":"2"}
不应该在那里。
.lt
查询也按预期工作。
任何想法为什么会这样?
提前致谢。
答案 0 :(得分:3)
问题出在时区...... mongoid created_at
字段存储在UTC
时区
您可以使用以下工作
Model.where(:created_at.gt => post_2_instance.created_at)
鉴于post_2_instance
为{"post":"2","created_at": "2014-03-25 13:04:44"}
但如果您想使用日期查询,可以使用以下
Model.where(:created_at.gt => DateTime.parse("2014-03-25 13:04:43").in_time_zone('UTC'))
将奇怪的行为报告给this JIRA issue