1

我正在使用 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 条记录的任何建议?

4

1 回答 1

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>
 ]
于 2015-04-23T21:20:37.190 回答