title: MongoDB limit() and sort() Optimization
body: <p>I am trying to understand why this is happening:</p>
  
  <pre><code>db.items.find({uid: {$in:[34, 54, 53,1,2,3,5,6,7]} }).limit(40).sort({_id:-1}).explain()
  </code></pre>
  
  <p>returns me:</p>
  
  <pre><code>"cursor" : "BtreeCursor _id_-1_uid_1 multi",
  "nscanned" : 167,
  "nscannedObjects" : 40,
  "n" : 40,
  ...
  </code></pre>
  
  <p>However, without the sort</p>
  
  <pre><code>db.items.find({uid: {$in:[34, 54, 53,1,2,3,5,6,7]} }).limit(40).explain()
  </code></pre>
  
  <p>returns me:</p>
  
  <pre><code>"cursor" : "BtreeCursor uid_1 multi",
  "nscanned" : 40,
  "nscannedObjects" : 40,
  "n" : 40,
  ...
  </code></pre>
  
  <p>So what I dont get is, why when I add the sort, it scans more documents? I am sorting by <em>id, also I have this index to try to improve things "</em>_id_-1_uid_1" , but still it scans a lot of documents.</p>
  
  <p>This is a huge issue in my case, because this is my local database, so just a few documents are been scanned, however in my live site, I have millons of documents been scanned for a reason which I dont know what it is.</p>
  
  <p>Can anyone explain me what is going on?</p>