title: Poor lookup aggregation performance
body: <p>I have two collections</p>
  
  <p>Posts:</p>
  
  <pre><code>{
      "_Id": "1",
      "_PostTypeId": "1",
      "_AcceptedAnswerId": "192",
      "_CreationDate": "2012-02-08T20:02:48.790",
      "_Score": "10",
      ...
      "_OwnerUserId": "6",
      ...
  },
  ...
  </code></pre>
  
  <p>and users:</p>
  
  <pre><code>{
      "_Id": "1",
      "_Reputation": "101",
      "_CreationDate": "2012-02-08T19:45:13.447",
      "_DisplayName": "Geoff Dalgas",
      ...
      "_AccountId": "2"
  },
  ...
  </code></pre>
  
  <p>and I want to find users who write between 5 and 15 posts.
  This is how my query looks like:</p>
  
  <pre><code>db.posts.aggregate([
      {
          $lookup: {
              from: "users", 
              localField: "_OwnerUserId",
              foreignField: "_AccountId", 
              as: "X"
          }
      },  
      {
          $group: {
              _id: "$X._AccountId", 
              posts: { $sum: 1 }
          }
      },   
      {
          $match : {posts: {$gte: 5, $lte: 15}}
      },  
      {
          $sort: {posts: -1 }
      },
      {
          $project : {posts: 1}
      }
  ])
  </code></pre>
  
  <p>and it works terrible slow. For 6k users and 10k posts it tooks over 40 seconds to get response while in relational database I get response in a split second.
  Where's the problem? I'm just getting started with mongodb and it's quite possible that I messed up this query.</p>