I'm migrating an existing Rails app to use MongoDB (with Mongoid), and I'm having some trouble figuring out how to do aggregations like you can do with MySQL.
Previously I had something like SELECT DATE(created_at) AS day, SUM(amount) AS amount GROUP BY day
, Which would return a collection that you can loop through in the template like this:
:day => '2011-03-01', :amount => 55.00
:day => '2011-03-02', :amount => 45.00
etc...
Does anyone know how to do that in Mongoid? The model is pretty straightforward:
class Conversion
include Mongoid::Document
include Mongoid::Timestamps
field :amount, :type => Float, :default => 0.0
...
# created_at generated automatically, standard Rails...
end
Thanks!
-Avishai