title: Return full document after an Aggregate on MongoDB
body: <p>I'm grouping a Collection on two properties ($user &amp; $game) to return the Document that matches the maximum of $score property</p>
  
  <p>Here's my method:</p>
  
  <p>• Sorting by score descending,<br>
  • grouping on (user/game),<br>
  • selecting each $first element on each property :  </p>
  
  <pre><code>db.session.aggregate(
      { $sort: {score: -1} },
      { $group: {
                      _id: { user: "$user", game: "$game"},
                      id: { $first: "$_id"},
                      score: { $first: "$score" }, 
                      user: { $first: "$user" },
                      game: { $first: "$game" },
                      startDate: { $first: "$startDate" },
                      .... &amp; all remaining properties
                  }
      },
      { $project: {_id: "$id", score: 1, user: 1, game: 1, startDate: 1, ... } }
  )
  </code></pre>
  
  <p>But I'm not satisfied with this since it seems a bit redondant and laborious. If I need one more property in my Collection schema, I'll need to add the property at several places.</p>
  
  <p>I'm sure there is a better method to do this. Any idea?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>My Schema is defined as (in javascript, with <code>Mongoose</code>) :</p>
  
  <pre><code>var sessionSchema = new Schema({
      user        : { type: Schema.ObjectId, ref: 'User' },
      game        : { type: Schema.ObjectId, ref: 'Game' },
      level       : Number,
      percent     : Number,
      score       : Number,
      startDate   : Date,
      endDate     : Date,
  });
  </code></pre>
  
  <p>There is lots of <code>User</code>s and for each <code>User</code>, several <code>Game</code>s. A <code>Session</code> is a <code>Game</code> played by a <code>User</code> at a given time. So a <code>User</code> can have many <code>Session</code>s for one <code>Game</code>. I just want the <code>Session</code> where the player made his hi-score.<br>
  Something like :  </p>
  
  <pre><code>[ {_id: ..., user: u1, game: g1, score: 100, ...},
    {_id: ..., user: u2, game: g1, score: 120, ...},
    {_id: ..., user: u1, game: g2, score: 90, ...},
    ...
  ]
  </code></pre>
  
  <p>Note I already have something which works. I just wonder if there is something more <em>elegant</em>.</p>