Mongodb 2.4.8、Mongoid 4.0.2、Rails 4.2.1 を使用しています。
mongodb を使用したテスト データで正しい結果セットを返すために、mongoid を介して実行される日付範囲クエリを取得しようとしています。5 ~ 7 日間にまたがるドキュメントを 6 つだけ作成しました。たとえば、次のクエリは、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
これらはクエリです。これまでに試したところ、22 日から 27 日の日付の間に 3 つのレコードではなく、すべて 1 つのレコードが返されました。
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 つではなく 1 つのレコードを返します。
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 つのレコードを返す方法について何か提案はありますか?