Date in Mongoid queries

10,701

I would recommend not using Date, but instead DateTime:

field :set_date, :type => DateTime

Now not only will it be stored in 1 field, like so:

"set_date" : ISODate("2012-03-14T17:42:27Z")

But Mongoid will correctly handle various conversions for queries like you want:

SimpleAction.where( :set_date => { :$lte => Date.today } )
Share:
10,701
ceth
Author by

ceth

I'm a programmer, occasional SRE, Unix automator. I'm currently working primarily in Go, and prior to that my weapon of choice was Python, but I'm also familiar with Java, C#, JavaScript, and bash. I dabble in Clojure and F# but I've never thrown a big problem at it. I run Linux at home and at work but that doesn't mean I'm ignorant of other systems :)

Updated on June 04, 2022

Comments

  • ceth
    ceth almost 2 years

    I have a model:

    class SimpleAction
      include Mongoid::Document
      field :set_date, :type => Date
    

    and I have some data in collection:

    { "_id" : ObjectId("4f6dd2e83a698b2518000006"), "name" : "lost", "notes" : "", "set_date(1i)" : "2012", "set_date(2i)" : "3", "set_date(3i)" : "25", "set_date(4i)" : "13", "set_date(5i)" : "57", "duration" : 15, "todo" : "4" }

    You can see that mongoid store date in the five fields - set_date(ni).

    I have two question:

    • How can I filter data by set_date field in the mongo console client? Something like this:

      db.simple_actions.find({ set_date : { "$lte" : new Date() } })
      

      My query didn't return any data.

    • How can I filter data by set_date field in my Rails controller? Something like this:

      @simple_actions = SimpleAction.where(:set_date => { '$lte' => Date.today })
      
  • ceth
    ceth about 12 years
    Hmm.. I changed model difinition and insert new recodrs in the collection, but nothing changed.
  • ceth
    ceth about 12 years
    Here is form fragment: <%= f.datetime_select :set_date %>
  • rfunduk
    rfunduk about 12 years
    Can you be more specific about 'nothing changed'? Clearly DateTime is a different field type than date, yes? :)
  • ceth
    ceth about 12 years
    For pure experiment I have created new model, controller and view by 'rails g scaffold Testing set_date:datetime'. Then I created records and look at them on mongodb - set_date field consists of 5 parts. I have changed Time type to DateTime, created another 2 new records and look at them on mongodb - new records have five-segments on set_date field (set_date(ni)).
  • ceth
    ceth about 12 years
    But the set_date field set correctly if I set the line '@testing.set_date = Time.now' into controller. As I see, the problem is into view and datetime_select helper
  • rfunduk
    rfunduk about 12 years
    What version of Mongoid/MongoDB are you using? I have a large Mongoid+Rails3 project that doesn't show this behavior, and I made a quick test project just now that also doesn't...
  • ceth
    ceth about 12 years
    Rails - 3.2.1, Mongoid - 2.4.6. It is the datetime_select helper problem. I have changed it to text_field and all works fine.
  • rfunduk
    rfunduk about 12 years
    Odd, well, datetime_select is kind of weak anyway right? Probably want to replace it at some point with a fancy little widget of some kind... sounds like you should just do that part now :)