使用mongodb mongoid rails查询最近7或30天的日期范围

时间:2015-04-23 09:27:23

标签: ruby-on-rails mongodb ruby-on-rails-4 mongoid mongoid4

我正在使用Mongodb 2.4.8,Mongoid 4.0.2和Rails 4.2.1

我一直在尝试通过mongoid获取日期范围查询,以便使用mongodb在测试数据上返回正确的结果集。我创建了6个文档,跨越了5到7天。例如,下面的查询应该返回22日至27日之间的3条记录,而不是返回一条记录:

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

这些是我迄今为止尝试的查询,它们都在第22和第27日之间返回1条记录而不是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条记录的任何建议吗?

1 个答案:

答案 0 :(得分:0)

这终于得到了解决。我需要将日期存储在变量中,然后以这种方式查询:

Person.first.person_email_clicks.gt(date: u).lt(date: e). 

或以这种方式查询:

Person.first.person_email_clicks.where({:date.gt =>  u, :date.lt  =>  e})

这是获得理想结果的完整步骤:

u = Person.first.person_email_clicks.first.date
   => Wed, 22 Apr 2015 22:12:00 +0000

e = Person.first.person_email_clicks.last.date
   => Sat, 25 Apr 2015 22:12:00 +0000

h = Person.first.person_email_clicks.gt(date: u).lt(date: e).to_a

返回了2条记录,如下所示:

 => [
      #<PersonEmailClick _id: 55381d536461760b6c010000, name: "seller", date: 2015-04-23 01:12:00 UTC, daily_total: 0, email_open: 0, page_views: 0, clicks: 0, visits: 0>, 

      #<PersonEmailClick _id: 55381d916461760b6c020000, name: "giver", date: 2015-04-24 22:12:00 UTC, daily_total: 0, email_open: 0, page_views: 0, clicks: 0, visits: 0>
 ]