'Pagination: How to know if there are more documents in a mongoDB collection?

How can I know if there're more documents in mongo query that implements limit()

Is there a way to know if there are more documents available for pagination in MongoDB?. Following the example shared in this post, I somehow still end up getting a false flag but I am sure there's a next page

  let pagelimit = number || 10
  const items = db.find({}).sort({ _id: 1 }).limit(pagelimit).toArray()
 
  items.length > page limit + 1 // false
  items.length > page limit // false

Tried items.length > page limit + 1 and items.length > page limit they both still return a false but there are definitely more items in the db.

Any pointers on how to go about this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source