title: Mongo Aggregate $match after ($sort->$limit)
body: <p>Consider the following mongo collection "events":</p>
  
  <pre><code>{ "_id" : ObjectId("512bc95fe835e68f199c8686"), userId: 1, type: "music" eventNum: 1 }
  { "_id" : ObjectId("512bc962e835e68f199c8687"), userId: 1, type: "music" eventNum: 2 }
  { "_id" : ObjectId("55f5a192d4bede9ac365b257"), userId: 2, type: "music" eventNum: 3 }
  { "_id" : ObjectId("55f5a192d4bede9ac365b258"), userId: 2, type: "music" eventNum: 4 }
  { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), userId: 1, type: "music" eventNum: 5 }
  { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), userId: 1, type: "athletic" eventNum: 6 }
  { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), userId: 2, type: "athletic" eventNum: 7 }
  </code></pre>
  
  <p>Each event is created with a <code>userId</code>, a <code>type</code> and an <code>eventNum</code>. I need to find the top 3 events of <code>userId: 1</code>. So I run this query:</p>
  
  <pre><code>db.getCollection('events').aggregate([
    {
      "$match": {
        "userId": 1
      }
    },
    {
      "$sort": { "eventNum": 1 }
    },
    {
      "$limit": 3
    }
  ])
  </code></pre>
  
  <p>Which returns the dataset (NOTE there are no "athletic" events returned):</p>
  
  <pre><code>{ "_id" : ObjectId("512bc95fe835e68f199c8686"), userId: 1, type: "music" eventNum: 1 }
  { "_id" : ObjectId("512bc962e835e68f199c8687"), userId: 1, type: "music" eventNum: 2 }
  { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), userId: 1, type: "music" eventNum: 5 }
  </code></pre>
  
  <p>Now, however, I want all "athletic" events of <code>userId: 1</code>, but ONLY if they are in the top 3. Since there are no "athletic" events in the top 3, we would expect the following query to return no documents:</p>
  
  <pre><code>db.getCollection('events').aggregate([
    {
      "$match": {
        "userId": 1
      }
    },
    {
      "$sort": { "eventTime": 1 }
    },
    {
      "$limit": 3
    },
    {
      "$match": {
        "type": "athletic"
      }
    }
  ])
  </code></pre>
  
  <p>HOWEVER, this query actually returns this dataset:</p>
  
  <pre><code>{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), userId: 1, type: "athletic" eventNum: 6 }
  </code></pre>
  
  <p>Can someone explain what is going on here? It seems that the sort/limit is happening after the second match. Is there anyway to get around this without making multiple queries?</p>