title: MongoDB - Mongoid map reduce basic operation
body: <p>I have just started with MongoDB and mongoid.
  The biggest problem I'm having is understanding the map/reduce functionality to be able to do some very basic grouping and such.</p>
  
  <p>Lets say I have model like this:</p>
  
  <pre><code>class Person
    include Mongoid::Document
    field :age, type: Integer
    field :name
    field :sdate
  end
  </code></pre>
  
  <p>That model would produce objects like these:</p>
  
  <pre><code>#&lt;Person _id: 9xzy0, age: 22, name: "Lucas", sdate: "2013-10-07"&gt;
  #&lt;Person _id: 9xzy2, age: 32, name: "Paul", sdate: "2013-10-07"&gt;
  #&lt;Person _id: 9xzy3, age: 23, name: "Tom", sdate: "2013-10-08"&gt;
  #&lt;Person _id: 9xzy4, age: 11, name: "Joe", sdate: "2013-10-08"&gt;
  </code></pre>
  
  <p>Could someone show how to use mongoid map reduce to get a collection of those objects grouped by the sdate field? And to get the sum of ages of those that share the same sdate field?</p>
  
  <p>I'm aware of this: <a href="http://mongoid.org/en/mongoid/docs/querying.html#map_reduce" rel="noreferrer">http://mongoid.org/en/mongoid/docs/querying.html#map_reduce</a>
  But somehow it would help to see that applied to a real example. Where does that code go, in the model I guess, is a scope needed, etc.</p>
  
  <p>I can make a simple search with mongoid, get the array and manually construct anything I need but I guess map reduce is the way here. And I imagine these js functions mentioned on the mongoid page are feeded to the DB that makes those operations internally. Coming from active record these new concepts are a bit strange. </p>
  
  <p>I'm on Rails 4.0, Ruby 1.9.3, Mongoid 4.0.0, MongoDB 2.4.6 on Heroku (mongolab) though I have locally 2.0 that I should update.</p>
  
  <p>Thanks.</p>