title: mongodb how to get a document which has max value of each "group with the same key"
body: <p>I have a collection:</p>
  
  <pre><code>{'_id':'008','name':'ada','update':'1504501629','star':3.6,'desc':'ok', ...}
  {'_id':'007','name':'bob','update':'1504501614','star':4.2,'desc':'gb', ...}
  {'_id':'005','name':'ada','update':'1504501532','star':3.2,'desc':'ok', ...}
  {'_id':'003','name':'bob','update':'1504501431','star':4.5,'desc':'bg', ...}
  {'_id':'002','name':'ada','update':'1504501378','star':3.4,'desc':'no', ...}
  {'_id':'001','name':'ada','update':'1504501325','star':3.6,'desc':'ok', ...}
  {'_id':'000','name':'bob','update':'1504501268','star':4.3,'desc':'gg', ...}
  ...
  </code></pre>
  
  <p>if I want the result is, the max value of 'update' of the same 'name', means the newest document of 'name', get the whole document:</p>
  
  <pre><code>{'_id':'008','name':'ada','update':'1504501629','star':3.6,'desc':'ok', ...}
  {'_id':'007','name':'bob','update':'1504501614','star':4.2,'desc':'gb', ...}
  ...
  </code></pre>
  
  <p><strong>How to do it most effective?</strong></p>
  
  <p>I do it now in python is:</p>
  
  <pre><code>result=[]
  for name in db.collection.distinct('name'):
      result.append(db.collection.find({'name':name}).sort('update',-1)[0])
  </code></pre>
  
  <p>is it do 'find' too many times?</p>
  
  <p>=====</p>
  
  <p>I do this for crawl data with 'name', get many other keys, and every time I insert a document, I set a key named 'update'.
  When I using the database, I want the newest <strong>document</strong> of specific 'name'. so it looks can not just use $group.
  How should I do? re design the db structure or better way to find?</p>
  
  <p>=====</p>
  
  <p>Improved !<br>
  I've tried create index of 'name' &amp; 'update', the process is shortened from half hour to 30 seconds!</p>
  
  <p>But I still welcome for better solution ^_^</p>