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 })
I would recommend not using
Date, but insteadDateTime:Now not only will it be stored in 1 field, like so:
But Mongoid will correctly handle various conversions for queries like you want: