Bashar Abdullah
Bashar Abdullah

Reputation: 1545

Date Querying in Mongoid

This has been asked a lot, but I'm still facing some issues with it.

I have date I receive and was storing as Date. I need to query on Greater Than and Less than, so I changed it to Time, tried it again, but I'm getting weird results.

I'm doing this

Class.where(:event_date.gt => Time.parse(Date.today))

and I'm getting old records, 1940s, 1960s and others. I tried converting time by adding .utc at the end, comparing against Date.today only, but nothing sovled the problem so far. This is the selector being generated by Mongoid

selector: {:date_utc=>{"$gte"=>Sat Sep 10 21:00:00 UTC 2011}},

I receive the date in this format "2011-09-11" and store it in the Time field. Tried parsing that as Time utc as well, no luck.

Any idea? I'm using Mongoid 2.0.2. Later versions seem incompatible with other extensions I'm using.

[UPDATE]

So the problem is with the old dates before 1970 apparently. How do I deal with them is the question now.

Upvotes: 8

Views: 4886

Answers (1)

Javier Ferrero
Javier Ferrero

Reputation: 8811

This is a known bug in Mongo. See ISSUE 405

The reason is that Mongo uses an unsigned number to store dates, so anything before the epoch rolls over far into the future.

Affortunately this issue has been fixed for stable version 2.0 released today. Upgrading to this version should solve your problem.

Upvotes: 2

Related Questions