I am working on a Volt app that needs to compare times. Using a model that looks like this:
class Punchcard < Volt::Model
field :punchtime
end
I can save instances of the model with a Time object for punchtime and see that they are persisted into MongoDB as ISODate() objects. All well so far.
When I want to query from the database using a less than criteria, I can see that Volt and volt-mongo are turning this call:
store.punchcards.where({"punchtime" => {"$lt" => punchtime}}).then...
(where "punchtime" is a Time object) into this Mongo query:
db.punchcards.find({"punchtime": {"$lt": "2015-07-08T10:25:05-0700"}})
Which returns no results even though it should.
When I trace the insert call when saving an instance of the Punchcard model, I see that the Time object is not stringified:
[INFO] task StoreTasks#save in 11.557ms
with args: "punchcards", ["punchcards", "[]"], {"user_id"=>"925b0eb84bc3d2185d1c693a", "punchtime"=>2015-07-08 16:38:49 -0700, "punch_status"=>"Out", "id"=>"75c1a3c35016d30895d38d28"}
Therefore, I'm guessing since the Time object is stringified on the find
call but not on the save call, Mongo is doing a string comparison on the find
.
How can I query Mongo with $lt / $gt and Time objects in Volt?
This should be fixed on master. Try running from master and let me know if it works.
gem 'volt', github: 'voltrb/volt'