title: pymongo / mongodb advanced query
body: <p>I'm a novice in pymongo/mongodb and now I have a challenge.</p>
  
  <p>I have the following structure stored in mongodb (v 2.04).</p>
  
  <pre><code>{
      "t": &lt;timestamp&gt;, 
      "d": {
          "uid": &lt;string&gt;,
          "action": &lt;string&gt;
      }
  }
  </code></pre>
  
  <p>This structure tracks user actions and is slightly reduced in complexity from my original.
  The data is pretty huge and the query will have a limiting datespan to reduce the results.</p>
  
  <p>What I would like is to be able to create a table of the users who did the most actions during a certain timespan.</p>
  
  <p>Table:</p>
  
  <pre><code>Rank    Uid    #num actions
  1       5      235
  2       237    234
  3       574    229
  </code></pre>
  
  <p>So far I've only bits and pieces of the query:</p>
  
  <pre><code>query = {"t": {"$lte": end_utc, "$gte": start_utc}}
  db.actions.find(query).distinct("d.uid")
  </code></pre>
  
  <p>Which will simply produce a list of unique uid's.
  How can I query (using pymongo) to get a list such as:</p>
  
  <pre><code>[
      {
          "actions": 100,
          "uid": 273
      },
      {
          "actions": 99",
          "uid": 632
      }..n sorted on actions descending
  
  ]
  </code></pre>