title: MongoDB {aggregation $match} vs {find} speed
body: <p>I have a mongoDB collection with millions of rows and I'm trying to optimize my queries. I'm currently using the aggregation framework to retrieve data and group them as I want. My typical aggregation query is something like : <code>$match &gt; $group &gt; $ group &gt; $project</code></p>
  
  <p>However, I noticed that the last parts only take a few ms, the beginning is the slowest.</p>
  
  <p>I tried to perform a query with only the $match filter, and then to perform the same query with collection.find. The aggregation query takes ~80ms while the find query takes 0 or 1ms.</p>
  
  <p>I have indexes on pretty much each field so I guess this isn't the problem. Any idea on what could go wrong ? Or is it just a "normal" drawback of the aggregation framework ?</p>
  
  <p>I could use find queries instead of aggregation queries, however I would have to perform a lot of processing after the request and this process can be done quickly with <code>$group</code> etc. so I would rather keep the aggregation framework.</p>
  
  <p>Thanks,</p>
  
  <p>EDIT :</p>
  
  <p>Here is my criteria :</p>
  
  <pre><code>{
      "action" : "click",
      "timestamp" : {
              "$gt" : ISODate("2015-01-01T00:00:00Z"),
              "$lt" : ISODate("2015-02-011T00:00:00Z")
      },
      "itemId" : "5"
  }
  </code></pre>