使用 mongodb mongoid rails 查询最近 7 天或 30 天的日期范围
Querying for last 7 or 30 days date range with mongodb mongoid rails
我正在使用 Mongodb 2.4.8、Mongoid 4.0.2 和 Rails 4.2.1
我一直在尝试通过 mongoid 获取日期 运行ge 查询 运行 到 return 测试数据的正确结果集 mongodb。我只创建了 6 个文档,时间跨度超过 5 - 7 天。例如,下面的查询应该在 22 日到 27 日之间 return 3 条记录,而不是 return 一条记录:
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
这些是查询,到目前为止我已经尝试过了,它们都是 return 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 个查询 returns 一条记录而不是 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)
关于如何将此调整为 return 22 日至 27 日 运行ge 之间的 3 条记录的任何建议?
终于解决了。我需要将日期存储在一个变量中,然后这样查询:
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>
]
我正在使用 Mongodb 2.4.8、Mongoid 4.0.2 和 Rails 4.2.1
我一直在尝试通过 mongoid 获取日期 运行ge 查询 运行 到 return 测试数据的正确结果集 mongodb。我只创建了 6 个文档,时间跨度超过 5 - 7 天。例如,下面的查询应该在 22 日到 27 日之间 return 3 条记录,而不是 return 一条记录:
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
这些是查询,到目前为止我已经尝试过了,它们都是 return 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 个查询 returns 一条记录而不是 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)
关于如何将此调整为 return 22 日至 27 日 运行ge 之间的 3 条记录的任何建议?
终于解决了。我需要将日期存储在一个变量中,然后这样查询:
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>
]