How do I go about querying a date range (of say the last 30 days from now) with Mongoid and Ruby?
I need to end up with an array or hash like the following:
{
15 => 300,
14 => 23,
13 => 23
...
30 => 20 # Goes over into previous month
28 => 2
}
I am currently storing each document with a DateTime instance as well as a unix timestamp Integer field.
The keys in the above hash are the days and the values are the sum of all sales for those days.
Any ideas?
Here’s how to do it all in rubyland:
This will create a hash with “month-day” keys, reason is that some months have fewer than 30 days and will result in a key collision if the query is always 30.
If you want a different range, change the query:
Change
created_atto whatever the name of your datetime field is.