How to do pagination using range queries in MongoDB?

前端 未结 2 849
谎友^
谎友^ 2020-12-02 23:57

I\'m probably missing something since I\'m still learning the ins and outs of MongoDB, but I need help with paging a collection.

I have a collection that has a lis

2条回答
  •  借酒劲吻你
    2020-12-03 00:22

    You just need to call skip on the cursor

    see MongoDB documentation on cursor skip "This approach may be useful in implementing “paged” results".

    This example is taken from Mongodb's example

    function printStudents(pageNumber, nPerPage) {
       print("Page: " + pageNumber);
       db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "

    "); } ); }

提交回复
热议问题