title: mongodb aggregate framework query with max date
body: <p>I have few records like this </p>
  
  <pre><code>userid purchased  brand
  1      2012-1-21  honda
  1      2013-1-1   toyota
  1      2013-2-30  mercedez
  
  2      2013-1-1   honda
  2      2012-1-1   toyota
  2      2013-12-13 mercedez
  </code></pre>
  
  <p>I want to get the user record with max purchased date. like following</p>
  
  <pre><code>1      2013-2-30  mercedez
  2      2013-12-13 mercedez
  </code></pre>
  
  <p>I m trying to user mongodb aggregate frame work but having problem with the correct query.</p>
  
  <pre><code>db.carsrecord.aggregate([{"$match" :{"userid":{"$in":[1,2,3,11]}}} ,
      {"$group":{"_id": {"uid":"$userid"},timecreated:{"$max":"$purchased" }}} ])
  </code></pre>
  
  <p>it returns me correct hid and with time but not returning the car brand. </p>
  
  <pre><code>"result" : [
      {
          "_id" : {
              "uid" : 1
          },
          "timecreated" : ISODate("2013-10-03T20:01:45.366Z")
      }
  ],
  "ok" : 1
  </code></pre>
  
  <p>Thanks</p>