- Kindly ensure you’ve gone through the earlier chapter of this article. mongodb-query-filter-sort-limit-pagination
- You can also get the example data used in this article. tours.json
http://localhost:3000/api/v1/tours?fields=name,durations,price, Query the data which only return name, durations and price
http://localhost:3000/api/v1/tours?limit=10&page=2, Query the data which skip the first 10 results
Field Limiting (start from line 22)
- If the ‘fields’ query paramater is existed, split the parameter from comma to white-space so that it can read and limit multiple fields
- Apply mongoose
selectmethods to only return the selected field.
- Exclude out the mongo version field ‘__v’ by simply put a negative sign (-) infront
Pagination (start from line 31)
- Pagination is very useful implementation when you are dealing with a very large dataset.
.limitmethods allow us to achieve pagination.
.skip: total records we wanted to skip from the query
.limit: total records we wanted to show from the query
- As we are passing the Page Number
pageand Total Records
limitfrom the query parameters. We can then come out with the formula
const skip = (page - 1) * limit. This will let us know how mnay records we are required to skip and paginate to our desire page number