我在轨道上使用mongoid和ruby.
我使用mongoid存储created_at时间
@H_403_4@include Mongoid::Timestamps所以,假设我有3个帖子就是这样创建的
@H_403_4@{"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” }
@H_403_4@Model.where(:created_at.gt => "2014-03-25 13:04:44")但是在上面的查询中它返回{“post”:“2”}和{“post”:“3”},所以{“post”:“2”}不应该在那里.
此.lt查询按预期工作.
任何想法为什么会这样?
提前致谢.
解决方法
问题出在时区…… mongoid created_at字段存储在UTC时区
你可以使用以下工作
@H_403_4@Model.where(:created_at.gt => post_2_instance.created_at)鉴于post_2_instance是{“post”:“2”,“created_at”:“2014-03-25 13:04:44”}
@H_403_4@Model.where(:created_at.gt => DateTime.parse("2014-03-25 13:04:43").in_time_zone('UTC'))奇怪的行为报告到this github issue