Pagination & Filtering
- Pagination, filtering, and sorting help APIs efficiently manage and return structured data from large datasets.
- 🔹 Pagination Techniques
Pagination is the process of dividing large datasets into smaller, manageable pages so the client receives limited data per request.🧠 Why Pagination is Important
Faster API responses
Reduced server load
Better UI performance (especially tables & lists)
Essential for real-world applications
🔹 Common Pagination Parameters
🧪 Example – Basic Pagination (MongoDB + Mongoose)
Pagination Techniques in APIs
Pagination divides large datasets into smaller pages to improve API performance, reduce server load, and deliver faster, user-friendly responses using parameters like page, limit, and skip.
const getUsers = async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const skip = (page - 1) * limit;
const users = await User.find()
.skip(skip)
.limit(limit);
res.json({
page,
limit,
totalUsers: await User.countDocuments(),
data: users
});
};
Explanation:
page=1&limit=10 → First 10 records
page=2&limit=10 → Next 10 records
🔹 Filtering Data
Filtering allows clients to retrieve specific records based on conditions like name, status, category, or price.
Example – Filtering by Field
Filtering Data in APIs
Filtering allows clients to fetch specific records based on query parameters like category or availability, returning only relevant data from the server.
const getProducts = async (req, res) => {
const filter = {};
if (req.query.category) {
filter.category = req.query.category;
}
if (req.query.inStock) {
filter.inStock = req.query.inStock === "true";
}
const products = await Product.find(filter);
res.json(products);
};
- Request Example:
/api/products?category=electronics&inStock=true
- 🔹 Sorting Data
Sorting arranges data in ascending or descending order based on fields like date, price, or name.Example – Sorting with Query Params
Sorting Data in APIs
Sorting organizes API response data in ascending or descending order using query parameters, allowing clients to control how results are displayed.
const sortField = req.query.sortBy || "createdAt";
const sortOrder = req.query.order === "asc" ? 1 : -1;
const products = await Product.find()
.sort({ [sortField]: sortOrder });
res.json(products);
- Request Example:
/api/products?sortBy=price&order=asc
- 🔹 Combining Pagination + Filtering + Sorting
Real-World API Pattern
const getAllProducts = async (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 5;
const skip = (page - 1) * limit;
const filter = {};
if (req.query.category) {
filter.category = req.query.category;
}
const sortBy = req.query.sortBy || "createdAt";
const order = req.query.order === "asc" ? 1 : -1;
const products = await Product.find(filter)
.sort({ [sortBy]: order })
.skip(skip)
.limit(limit);
const total = await Product.countDocuments(filter);
res.json({
page,
totalPages: Math.ceil(total / limit),
totalRecords: total,
data: products
});
};