title: Mongodb : find max and distinct
body: <p>In MongoDB I have some documents such as these :</p>
  
  <pre><code>{type:'a', ts:123, foo:'bar'}
  {type:'a', ts:456, foo:'baz'}
  {type:'a', ts:789, foo:'whee'}
  {type:'b', ts:123, foo:'jingle'}
  {type:'b', ts:456, foo:'bells'}
  {type:'b', ts:789, foo:'yeehaaw'}
  </code></pre>
  
  <p>I would like to find the documents which have the max ts value for each type.
  Is it possible to do that with a disngle query ? 
  I've looked at distinct but apparently it only returns the disting 'type' values.
  So I guess I would need a combination of $max alongside distinct ... 
  But I can't figure the proper way.</p>
  
  <p>Thanks</p>