Skip to content

Instantly share code, notes, and snippets.

@durran
Created April 15, 2010 21:24
Show Gist options
  • Select an option

  • Save durran/367682 to your computer and use it in GitHub Desktop.

Select an option

Save durran/367682 to your computer and use it in GitHub Desktop.
First we create a document with a pre-epoch Time in Ruby:
>> time = Date.new(1950, 1, 1).to_time
=> Sun Jan 01 00:00:00 -0400 1950
>> time.class
=> Time
>> time.to_i
=> -631137600
>> collection = Mongoid.master.collection("driver_test");
>> collection.insert({ "dob" => time })
=> ObjectID('4bc783b6f23ada04ca000001')
Conversion back from db is fine:
>> id = _
=> ObjectID('4bc783b6f23ada04ca000001')
>> collection.find_one({ "_id" => id })
=> {"_id"=>ObjectID('4bc783b6f23ada04ca000001'), "dob"=>Sun Jan 01 04:00:00 UTC 1950}
>> doc = _
=> {"_id"=>ObjectID('4bc783b6f23ada04ca000001'), "dob"=>Sun Jan 01 04:00:00 UTC 1950}
>> doc["dob"]
=> Sun Jan 01 04:00:00 UTC 1950
But looking at the value that got stored in the database:
> doc = db.driver_test.findOne();
{
"_id" : ObjectId("4bc783b6f23ada04ca000001"),
"dob" : "Tue Jan -2147483647 584556000 15:25:52 GMT-0300 (CLST)"
}
> doc.dob.getTime();
18446743442571952000
Our queries are busted...
> db.driver_test.findOne({ "dob" : { $lt : new Date() } });
null
> db.driver_test.findOne({ "dob" : { $lte : new Date() } });
null
> db.driver_test.findOne({ "dob" : { $gte : new Date() } });
{
"_id" : ObjectId("4bc783b6f23ada04ca000001"),
"dob" : "Tue Jan -2147483647 584556000 15:25:52 GMT-0300 (CLST)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment