mongodbpymongotornado-motor

Check if there are more documents when implementing pagination with skip() and limit()


I'm using Motor, which uses pymongo under the hood, to implement pagination for some documents. Here is what I have:

    rows = (
        db.programs
        .find(query)
        .sort("_id")
        .skip(page * page_count)
        .limit(page_count)
    )

What's the recommended way to check if there are more documents?

rows.next_object() is always returning None, even when there are more documents to fetch.


Solution

  • You can limit to page_count+1 documents. If you get that many, you know there is another page of results to show. If you get page_count or fewer, you are on the last page.