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.
the Date() Method in MongoDB In MongoDB shell or mongosh, the Date() method returns the current date as a string. The new Date() function returns a Date object with the current date. The ISODate helper is wrapped around the Date object by mongosh, and the ISODate is in UTC (Universal Time).
Comparison Based on Date in MongoDB First, create a collection called 'data' using the document to further understand the concept. Use the find() function to show all the documents in a collection. The following is the date-based return query. Records with a creation date after 2018-05-19T11:10:23Z will be returned.
The DATE type in MongoDB can store date and time values as a combined unit. The BSON Date type is a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With