title: Find distinct documents with max value of a field in mongodb
body: <p>I have thousands of documents in MongoDB with some of the sample as below:</p>
  
  <pre><code>{"title":"Foo", "hash": "1234567890abcedf", "num_sold": 49, 
  "created": "2013-03-09 00:00:00"}
  
  {"title":"Bar", "hash": "1234567890abcedf", "num_sold": 55, 
  "created": "2013-03-11 00:00:00"}
  
  {"title":"Baz", "hash": "1234567890abcedf", "num_sold": 55,
   "created": "2013-03-10 00:00:00"}
  
  {"title":"Spam", "hash": "abcedef1234567890", "num_sold": 20,
   "created": "2013-03-11 00:00:00"}
  
  {"title":"Eggs", "hash": "abc1234567890def", "num_sold": 20,
   "created": "2013-03-11 00:00:00"}
  </code></pre>
  
  <p>Is it possible to select all documents with distinct <code>hash</code> which has the max of <code>num_sold</code> and if there is more than one document with same <code>num_sold</code>, select the latest document from the <code>created</code> field. </p>
  
  <p>I use PyMongo for the client.</p>