title: MongoDB: How to group by field and get the most recent result?
body: <p>I have the following data in a MongoDB collection</p>
  
  <pre><code>[
    {name: "weight", value: 95,  date: "2014-11-07"},
    {name: "hips",   value: 105, date: "2014-02-06"},
    {name: "weight", value: 89,  date: "2014-05-12"}
  ]
  </code></pre>
  
  <p>I am trying to get the following data back:</p>
  
  <p>The <strong>value</strong> of each <strong>name</strong> with the most recent <strong>date</strong>:</p>
  
  <pre><code>[
    {_id: "weight", value: 95,  date: "2014-11-07"},
    {_id: "hips",   value: 105, date: "2014-02-06"}
  ]
  </code></pre>
  
  <p>I've looked into aggregation:</p>
  
  <pre><code>group: {
    _id: "$name",
    date: { $max: "$date" }
  }
  </code></pre>
  
  <p>But i am not getting the result that i want. I need the <strong>value</strong> of what was found </p>
  
  <pre><code>[
    {_id: "weight", date: "2014-11-07"},
    {_id: "hips",   date: "2014-02-06"}
  ]
  </code></pre>
  
  <p>How do I ask mongo to return the value in this aggregation? or is aggregation the wrong approach?</p>