![]() |
VOOZH | about |
Creating pagination in Node.js using the skip and limit methods. This approach efficiently retrieves specific data subsets from a database, improving performance and user experience by loading content in manageable segments rather than all at once.
Pagination is a very helpful method. This allows the client to fetch data in pages. This is achieved by using options skip and limit, giving clients full control of the page(data) they are getting back.
As its statement describes, Pagination should be used:
Step 1: Create a new folder for a project using the following command:
mkdir paginationStep 2: Navigate to our folder using the following command:
cd paginationStep 3: Initialize npm using the following command and server file:
npm init -y
touch index.js
Step 4: Install required packages using the following command:
npm i express mongoose Project Structure:
It will look like the following
👁 ImageExample 1: Without Using Pagination
Run the server using the following command:
node index.jsInserting into Database: Data is inserted with the help of Postman through the following method:
Output: Without Pagination
As it could be seen in the above example, Without pagination all the documents will get fetched. To be more clear about the use and need of Pagination. Think of this case when there are thousands of documents rather than just 4.
Example 2: With Using Pagination
For Pagination skip and limit, parameters will be used using the limit and skip method along with find.
Step to Run:Run the server using the following command:
node index.jsOutput: With Pagination