title: Mongoid Group By or MongoDb group by in rails
body: <p>I have a mongo table that has statistical data like the following....</p>
  
  <ul>
  <li>course_id </li>
  <li>status <code>which is a string, played or completed</code></li>
  <li>and timestamp information using <a href="http://mongoid.org/en/mongoid/docs/extras.html#timestamps" rel="nofollow">Mongoid's Timestamping feature</a></li>
  </ul>
  
  <p>so my class is as follows...</p>
  
  <pre><code>class Statistic
    include Mongoid::Document
    include Mongoid::Timestamps
    include Mongoid::Paranoia
  
    field :course_id, type: Integer
    field :status, type: String # currently this is either play or complete
  </code></pre>
  
  <p>I want to get a daily count of total # of plays for a course.  So for example...
  8/1/12 had 2 plays, 8/2/12 had 6 plays. Etc.  I would therefore be using the created_at timestamp field, with course_id and action.  The issue is I don't see a group by method in Mongoid. I believe mongodb has one now, but I'm unsure of how that would be done in rails 3.</p>
  
  <p>I could run through the table using each, and hack together some map or hash in rails with incrementation, but what if the course has 1 million views, retrieving and iterating over a million records could be messy.  Is there a clean way to do this?</p>