title: Occasional spike in nscanned for a MongoDB query
body: <p>I have a query of the form </p>
  
  <pre><code>db.coll.find({x: {$in: ['foo', 'bar', 'baz']}).sort({_id:-1}).limit(LIMIT)
  </code></pre>
  
  <p>"x" is a single ObjectId field, _id is an ObjectId too. There is an index {x:1, _id:-1}. This query should be hitting the fast path from <a href="https://jira.mongodb.org/browse/SERVER-5063%20#SERVER-5063" rel="nofollow">#SERVER-5063</a>. </p>
  
  <p>It does so most of the time, except some rare instances where it appears that a full table scan is performed - the profile entry for these queries shows nscanned to be slightly above the count of all documents matching <code>{x: {$in: ['foo', 'bar', 'baz']}}</code>. That's usually some 3 orders of magnitude above what the query scans in the expected case. </p>
  
  <p>The collection is fairly write heavy, so my first guess was that perhaps the query optimizer was retrying various query plans, including the <code>{_id:1}</code> index, but the spikes remain even after adding <code>.hint({x:1, _id:-1})</code>, so I'm not sure what's going on here.</p>
  
  <p>Can anyone explain the occasional scan of all documents matching the query and how to prevent it?</p>
  
  <p>edit: happens on MongoDB 2.4.4</p>