Querying for last 7 or 30 days date range with mongodb mongoid rails -


i using mongodb 2.4.8, mongoid 4.0.2 , rails 4.2.1

i have been trying date range query ran through mongoid return right set of results on test data mongodb. created 6 documents spanning on 5 - 7 days period. instance, query below, ought return 3 records days between 22nd - 27th, instead returns 1 record :

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'})  

this entire documents test purposes:

{"_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}     ] } 

here models:

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 

these queries, have tried far , return 1 record instead of 3 records between dates 22nd , 27th

 u = person.first.person_email_clicks.first.date  store datetime wed, 22 apr 2015 22:12:00 +0000   e = person.first.person_email_clicks.last.date  store the 2nd date mon, 27 apr 2015 22:12:00 +0000 

now use variables holding dates in query , 3 query below returns 1 record instead of 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) 

any suggestions on how tweak return 3 records between date range of 22nd - 27th ?

this has been resolved. needed stored date in variable , query way:

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

or query way:

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

this full step desired results:

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 

which returned 2 records shown below:

 => [       #<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>  ] 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -