Date in Mongoid queries
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 } )
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, 2022Comments
-
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 about 12 yearsHmm.. I changed model difinition and insert new recodrs in the collection, but nothing changed.
-
ceth about 12 yearsHere is form fragment: <%= f.datetime_select :set_date %>
-
rfunduk about 12 yearsCan you be more specific about 'nothing changed'? Clearly DateTime is a different field type than date, yes? :)
-
ceth about 12 yearsFor 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 about 12 yearsBut 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 about 12 yearsWhat 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 about 12 yearsRails - 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 about 12 yearsOdd, 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 :)