简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM