How to handle pagination queries properly with mongodb and php?

后端 未结 2 880
庸人自扰
庸人自扰 2021-01-31 05:12

Am I doing this right? I went to look at some old PHP code w/ MySQL and I\'ve managed to get it to work, however I\'m wondering if there\'s a much \"cleaner\" and \"faster\" way

2条回答
  •  爱一瞬间的悲伤
    2021-01-31 06:08

    Yes you are doing right. And you can run query in one shot.

    Here is a paging example:

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

    "); } ); }

    Reference: Advanced Queries - MongoDB: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-{{skip%28%29}}

提交回复
热议问题