Complete the code to set the default page number to 1 if not provided.
const page = parseInt(req.query.page) || [1];The default page number should be 1 to start pagination from the first page.
Complete the code to calculate the number of items to skip based on page and limit.
const skip = (page - 1) * [1];
We multiply (page - 1) by the limit to skip the correct number of items.
Fix the error in the MongoDB query to apply pagination correctly.
const results = await collection.find().skip([1]).limit(limit).toArray();The skip value must be calculated as (page - 1) * limit to get the correct starting point.
Fill both blanks to create a paginated API response with total pages and current page.
const totalPages = Math.ceil(totalItems / [1]); const response = { currentPage: [2], totalPages };
Total pages is total items divided by limit, rounded up. Current page is the page number requested.
Fill all three blanks to implement cursor-based pagination with limit and cursor parameters.
const query = {};
if ([1]) {
query._id = { $gt: [2] };
}
const results = await collection.find(query).limit([3]).toArray();If a cursor is provided, query for items with _id greater than cursor. Limit the results by the limit value.