乐闻世界logo
搜索文章和话题

Mongoose limit offset and count query

1个答案

1

When performing pagination queries with Mongoose, two parameters are typically required: page (current page number) and limit (number of records per page). These parameters enable us to calculate how many records to skip to retrieve the current page's data.

Here's a basic example demonstrating how to implement pagination:

Assume we have a model Item, and we need to implement a query to retrieve data for the page-th page, displaying limit records per page.

javascript
const page = req.query.page; // Retrieve page number from request const limit = req.query.limit; // Retrieve number of records per page from request const options = { page: parseInt(page, 10) || 1, // Set default page number to 1 limit: parseInt(limit, 10) || 10, // Set default number of records per page to 10 }; // Calculate number of records to skip const skip = (options.page - 1) * options.limit; // Pagination query Item.find() .skip(skip) .limit(options.limit) .exec((err, items) => { if (err) { // Handle error } else { // Handle query results } });

In the above code, we first retrieve the page number and number of records per page from the request. Then, we calculate the number of records to skip by subtracting 1 from the current page number and multiplying by the number of records per page. This value is the parameter required for the .skip() method. The .limit() method tells Mongoose how many records to retrieve per page.

Thus, when calling this pagination query, you only need to pass the corresponding page and limit parameters. If the user does not provide these parameters, we use default values.

It's worth noting that this method is suitable for pagination when the data volume is not very large. When handling large volumes of data, using .skip() and .limit() repeatedly may cause performance issues because .skip() actually reads and skips the preceding records, which can be slow when there are many records. One way to address this is to use a strategy similar to 'infinite scrolling,' retrieving the next batch of records based on the ID of the last loaded record.

2024年6月29日 12:07 回复

你的答案