RubyOnRails
RubyOnRails

Reputation: 235

Rails Limit results based on Date Range

I have the following Strucuture in the database

db.slots.find().pretty()
{
    "_id" : ObjectId("52ae8990bd521b2da7000003"),
    "created_at" : ISODate("2013-12-16T05:03:12.345Z"),
    "day_from" : "Mon",
    "day_to" : "Sat",
    "doctor_clinic_id" : ObjectId("52ae8990bd521b2da7000004"),
    "evening_time_from" : 0,
    "evening_time_from_period" : "AM",
    "evening_time_to" : 0,
    "evening_time_to_period" : "AM",
    "morning_time_from" : 9,
    "morning_time_from_period" : "AM",
    "morning_time_to" : 2,
    "morning_time_to_period" : "PM",
    "store" : [
        ISODate("2013-12-13T09:00:00Z"),
        ISODate("2013-12-13T09:15:00Z"),
        ISODate("2013-12-13T09:30:00Z"),
        ISODate("2013-12-13T09:45:00Z"),
        ISODate("2013-12-13T10:00:00Z"),
        ISODate("2013-12-13T10:15:00Z"),
        ISODate("2013-12-13T10:30:00Z"),
        ISODate("2013-12-13T10:45:00Z"),
        ISODate("2013-12-13T11:00:00Z"),
        ISODate("2013-12-13T11:15:00Z"),
        ISODate("2013-12-13T11:30:00Z"),
        ISODate("2013-12-13T11:45:00Z"),
        ISODate("2013-12-13T12:00:00Z"),
        ISODate("2013-12-13T12:15:00Z"),
        ISODate("2013-12-13T12:30:00Z"),
        ISODate("2013-12-13T12:45:00Z"),
        ISODate("2013-12-13T13:00:00Z"),
        ISODate("2013-12-13T13:15:00Z"),
        ISODate("2013-12-13T13:30:00Z"),
        ISODate("2013-12-13T13:45:00Z"),
........
.....
..
        ISODate("2013-12-25T13:15:00Z"),
        ISODate("2013-12-25T13:30:00Z"),
        ISODate("2013-12-25T13:45:00Z"),
        ISODate("2013-12-25T14:00:00Z")
    ],
    "updated_at" : ISODate("2013-12-16T05:03:12.345Z")
}

I want to only fetch the content of the Store which has the date of Today and the next five days.

When i try the following i get a single slot back

slots.where(:store.gte => Date.today)

Results

#<Mongoid::Criteria selector: {"doctor_clinic_id"=>"52ae8990bd521b2da7000004", "store"=>{"$gte"=>Mon, 16 Dec 2013}} options: {} class: Slot embedded: false> 

slot.rb

class Slot
  include Mongoid::Document
field :store, type: Array

  belongs_to :clinic
end

slot.store.where(:store.gte => Date.today) resulting one output like above..!!!

Upvotes: 0

Views: 186

Answers (1)

Enrique Fueyo
Enrique Fueyo

Reputation: 3488

First: What you are trying to achieve is called projection in MongoDB, which is used when you want to retrieve only certain fields from an element. Indeed what you are trying to do is retrieve certain elements from an array field (See Mongodb projection positional)

In mongo you try something like this:

query={}
projection = {day_from:1, day_to:1} //Retrieve only _id, day_from and day_to fields
db.slots.find(query,projection).pretty()

Mongodb projection positional is what you would need but the documentation says that it only returns the first element that matches the query.

So I guess that you cannot directly with mongo. I recommend you to try a different approach like a formatter and virtual attributes. Something similar to:

class Slot
  include Mongoid::Document
  field :store, type: Array

  def with_dates_after date
    self.virtual_store= self.store.select{|elem| elem >= date} 
  end

  def virtual_store= arg_arr
    @_virtual_store = arg_arr
  end

  def virtual_store
   @_virtual_store ||= []
  end

end

Upvotes: 1

Related Questions