Using MongoDB Limit() and Skip() together

10,161

Skipping five and then limiting result to ten means that fifteen items need to be considered, the first five will be skipped and the next ten will be returned.

So nscanned should properly be 15.

nscannedObjects should probably be 10, however, due to this bug/limitation, it's also shown as 15.

Share:
10,161
Ashish
Author by

Ashish

Updated on June 04, 2022

Comments

  • Ashish
    Ashish almost 2 years

    Here is the query:

    $collection->find(array("x"=>new MongoId("..."))->skip(5)->limit(10);
    

    Using explain gives the following results:

    [n]=>10
    [nscanned]=>15
    [nscannedObjects]=>15
    

    There is indexing on "x". So,if I am skipping the first 5 documents, why the number of scanned objects is 15 and not 10 ?

    • Asya Kamsky
      Asya Kamsky almost 12 years
      @hakre that's why nscanned should be 15. not nscannedObjects.
    • Ashish
      Ashish almost 12 years
      @hakre: please don't consider commenting if you don't understand the question. Look at Asya's answer. This is what I was expecting. This is a bug issue. And don't teach maths here. I know better maths than you.
    • hakre
      hakre almost 12 years
      On the server, the object has been loaded. The object is scanned. The skip operation is to differ in what is returned to client, but the server is reporting fine the object to be scanned. I don't see any problem with that and consider the bug report as bogus. I'd say usually mongodb developers are too lazy to clean up open tickets.
    • Ashish
      Ashish almost 12 years
      @hakre thnx. so isn't this inefficient to load the skipped documents in memory inspite of using indexing ? any solutions for this ?
    • hakre
      hakre almost 12 years
      @Ashish: Is it inefficient? Have you metriced this? Solution for what problem? The problem that you expect the wrong number?
    • Ashish
      Ashish almost 12 years
      @hakre: The problem is say, for eg., i have 10,000 documents. I am skipping the first 500 documents and then fetching 10 documents(501-510). According to your previous comment this would load all those 500 not required documents also in memory. So i am asking isn;t this inefficient ?
    • Asya Kamsky
      Asya Kamsky almost 12 years
      Ashish I believe @hakre is mistaken about what happens on the server. The object does not get loaded when there is an index that's usable.
    • hakre
      hakre almost 12 years
      @Ashish: You should differ between server and client memory. The server memory always represent the whole data of all documents, and a subset of that in RAM. you don't need to care about how that is organized because you're using this as a service. Sp yes, those 15 (and probably even more) are loaded into memory - but only on the server. What you have on the client is only those 10 doucments, and if you iterate over them, you can actually only have one of those 10 in memory if you wish so.
  • Ashish
    Ashish almost 12 years
    Okay. thnx. means the rest 5 are not loaded in memory. Right ??