One of the way to implement pagination is to use skip
and limit
methods:
const data = await db
.collection<any>('items-data')
.find({ model: model })
.sort({ 'time': 1 })
.skip(page > 0 ? ((page - 1) * PER_PAGE) : 0)
.limit(PER_PAGE)
.toArray();
- Filter out documents with
find
- Sort by a prop with
sort
- Skip either 0 or
page * PER_PAGE
- Limit the result with
limit
References: