Is cursor.skip() on indexed keys always faster?
<p>I have 2 databases: <code>slow</code> and <code>fast</code>; each of which was fed with 4096 entries. The <code>age</code> key is a unique random integer that was generated with this script:</p>

<pre><code>var arr = []
while(arr.length &lt; 4096){
  var randmnum=Math.ceil(Math.random()*1000000)
  var found=false;
  for(var i=0;i&lt;arr.length;i++){
    if(arr[i]==randmnum){found=true;break}
  }
  if(!found)arr[arr.length]=randmnum;
}

var i=0;
for (i=0 ; i&lt; arr.length; ++i) {
    db.fast.insert({name:"john doe", email:"once@upon.a.time.com", age:arr[i]});
    db.slow.insert({name:"john doe", email:"once@upon.a.time.com", age:arr[i]});
}
</code></pre>

<p>Then in mongo shell:</p>

<pre><code>&gt; db.createCollection("slow")
&gt; db.createCollection("fast")
&gt; load("theAboveScript.js")
&gt; db.fast.createIndex({"age":1})
</code></pre>

<p>If I test <code>find</code> and <code>sort</code> the amount of <code>work</code> that has been done on the <code>fast</code> db is lower than the <code>slow</code> as expected. Moreover the execution time is about 2 times better with the indexed/fast db.</p>

<p><strong>But</strong> when I do the following:</p>

<pre><code>&gt; pageNumber=18;nPerPage=20; db.slow.find().skip(pageNumber &gt; 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")
&gt; pageNumber=18;nPerPage=20; db.fast.find().skip(pageNumber &gt; 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")
</code></pre>

<p>The amount of work is exactly the same and the execution time is similar, even the fast db is a bit slower.</p>

<p>Why <a href="https://docs.mongodb.org/manual/reference/method/cursor.skip/" rel="nofollow">cursor.skip()</a> doesn't get any boost if it works on an indexed keys? I would expect the pagination to return the paged data ordered without sorting it explicitly.</p>