mongoose pagination returning the same document twice

ghz 8months ago ⋅ 101 views

I implement pagination by returning only two documents at a time. When paginating, I need to know if there are more documents to be fetched, or if the user is on the last page

Here is the existing code taken from This answer here

let pageCount = 2
let page = req.params.page

let result = await Chat.find()
    .skip(page * pageCount)
    .limit(pageCount + 1)

if (result.length === pageCount + 1) console.log("Has next page")
else console.log("On last page")

res.send(result)

This method works as expected as it correctly logs whether there is a next page or not. The issue I have is that when I increment the page, e.g from page 1 to page 2, I get returned duplicate documents. Documents which were also present on the previous page.

I tried changing the code for skip to skip(page * pageCount + 1), however this had no effect on fixing the issues.

Any ideas why this behavior is occurring, and any potential fixes? Thank you.

Answers

The issue you're encountering is likely due to the way you're calculating the skip value. When you increment the page, you should be skipping (page * pageCount) number of documents, not (page * pageCount + 1), as you correctly identified.

The reason you're getting duplicate documents is because the skip() function skips the specified number of documents, so if you skip one extra document (pageCount + 1) and then limit the result to pageCount, you'll end up with one overlapping document between pages.

To fix this issue, you can modify your code as follows:

let pageCount = 2
let page = req.params.page

let result = await Chat.find()
    .skip(page * pageCount) // Skip the correct number of documents
    .limit(pageCount + 1) // Fetch one extra document to check for next page

let hasNextPage = result.length > pageCount // Check if there are more documents than the page count
if (hasNextPage) console.log("Has next page")
else console.log("On last page")

res.send(result.slice(0, pageCount)) // Send only pageCount number of documents

In this modified code:

  • We first fetch pageCount + 1 documents to check if there is a next page.
  • Then, we check if the number of documents fetched is greater than pageCount. If it is, it means there are more documents to fetch on the next page.
  • Finally, we send only pageCount number of documents to the client by slicing the result array. This ensures that we don't return any duplicate documents.