Calculate Pagination Values
Offset-based pagination commonly accepts two query parameters:
- page: Requested page number.
- limit: Number of records per page.
The skip value is calculated using:
skip = (page - 1) * limit;
Example:
Page 1, limit 10:
skip = 0
Page 2, limit 10:
skip = 10
Page 3, limit 10:
skip = 20
Mongoose provides skip() and limit() to implement this behavior.
Calculate Pagination
const page = 2;
const limit = 10;
const skip = (page - 1) * limit;
console.log(skip); // 10Calculates the number of records to skip before retrieving the requested page.
Key Takeaways
page identifies the requested page.
limit determines records per page.
skip calculates the offset.