【发布时间】:2015-07-01 09:20:50
【问题描述】:
我正在使用 Mongodb 2.4.8、Mongoid 4.0.2 和 Rails 4.2.1
我一直在尝试通过 mongoid 运行日期范围查询,以使用 mongodb 返回测试数据的正确结果集。我只创建了 6 个文档,时间跨度超过 5 - 7 天。例如,下面的查询应该返回 22 日到 27 日之间的 3 条记录,而不是返回 1 条记录:
Person.where(:person_email_clicks.elem_match => {:date.gte => 'Wed, 22 Apr 2015 22:12:00 +0000'}, :person_email_clicks.elem_match => {:date.lte => 'Mon, 27 Apr 2015 22:12:00 +0000'})
这是用于测试目的的整个文档:
{"_id"=>BSON::ObjectId('55364a416461760bf1000000'),
"status"=>"premium"
"person_email_clicks"=>[
{"_id"=>BSON::ObjectId('55381d256461760b6c000000'), "name"=>"buyer", "date"=>2015-04-22 22:12:00 UTC},
{"_id"=>BSON::ObjectId('55381d536461760b6c010000'), "name"=>"seller", "date"=>2015-04-23 01:12:00 UTC},
{"_id"=>BSON::ObjectId('55381d916461760b6c020000'), "name"=>"giver", "date"=>2015-04-24 22:12:00 UTC},
{"_id"=>BSON::ObjectId('55381dac6461760b6c030000'), "name"=>"help", "date"=>2015-04-27 22:12:00 UTC},
{"_id"=>BSON::ObjectId('553824ba6461760b6c040000'), "name"=>"tt", "date"=>2015-04-22 22:12:00 UTC},
{"_id"=>BSON::ObjectId('553824bf6461760b6c050000'), "name"=>"tt", "date"=>2015-04-22 22:12:00 UTC},
{"_id"=>BSON::ObjectId('55382ad46461760b6c060000'), "name"=>"yyy", "date"=>2015-04-25 22:12:00 UTC}
]
}
以下是模型:
class Person
include Mongoid::Document
field :status, type: String
embeds_many :person_email_clicks
end
class PersonEmailClick
include Mongoid::Document
field :name, type: String
field :date, type: DateTime
embedded_in :person
end
这些是查询,到目前为止我已经尝试过,它们都返回 1 条记录,而不是 22 日和 27 日之间的 3 条记录
u = Person.first.person_email_clicks.first.date
We store the datetime which is Wed, 22 Apr 2015 22:12:00 +0000
e = Person.first.person_email_clicks.last.date
We store the the 2nd date which is Mon, 27 Apr 2015 22:12:00 +0000
现在我在查询中使用那些保存日期的变量,并且下面的所有 3 个查询都返回一条记录而不是 3:
f = Person.where(:person_email_clicks.elem_match => {:date.gte => u}, :person_email_clicks.elem_match => {:date.lte => e})
f = Person.where(:person_email_clicks.elem_match => {:date => {"lte" => u}}, :person_email_clicks.elem_match => {:date => {"gte" => e}})
t = Person.where('person_email_clicks.date' => u..e)
关于如何调整它以返回 22 日至 27 日之间的 3 条记录的任何建议?
【问题讨论】:
-
您的问题可能不在于查询的正确性...check this issue about mongoid querying dates in github
-
@artmees,感谢您的链接。
-
尝试使用以下查询(对我有用)
DateTime.parse('Wed, 22 Apr 2015 22:12:00 +0000').in_time_zone('UTC')其他日期格式无效(如问题所示)...但请确保使用正确的时区跨度> -
谢谢。我只是尝试了一个范围查询,它仍然返回了 1 条记录。这是我运行的查询 Person.where(:person_email_clicks.elem_match => {:date.gte => DateTime.parse('Wed, 22 Apr 2015 22:12:00 +0000').in_time_zone('UTC ')}, :person_email_clicks.elem_match => {:date.lte => DateTime.parse('Mon, 27 Apr 2015 22:12:00 +0000').in_time_zone('UTC')})
-
确定最后一件事,以确保您不会因为秒值而丢失一些记录...尝试如下
Person.where(:person_email_clicks.elem_match => {:date.gte => DateTime.parse('Wed, 22 Apr 2015 22:12:00 +0000').in_time_zone('UTC').beginning_of_day}, :person_email_clicks.elem_match => {:date.lte => DateTime.parse(' Mon, 27 Apr 2015 22:12:00 +0000').in_time_zone('UTC').end_of_day})注意 beginning_of_day 和 结束日
标签: ruby-on-rails mongodb ruby-on-rails-4 mongoid mongoid4