<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>Pagination Api Structure</h2>
</div>
</div>
2nd way to get count >>>>>>>>>>>>
const getPosts = async (req, res) => {
const { filter, pageNumber = 1, pageSize = 10 } = req.query;
try {
// Convert pageNumber and pageSize to integers
const page = parseInt(pageNumber);
const limit = parseInt(pageSize);
const skip = (page - 1) * limit;
// Match content using regex for partial matching if a filter is provided
const matchStage = filter
? { content: { $regex: filter, $options: 'i' } }
: {};
// Get total document count with filter
// const totalDocuments = await Post.countDocuments(matchStage);
// Get Posts with the filter, pagination, and user details
const result = await Post.aggregate([
{
$match: matchStage // Match stage to filter posts
},
{
$facet: {
totalCount: [
{ $count: "totalDocuments" } // Count total matching documents
],
paginatedData: [
{ $sort: { createdAt: -1 } }, // Sort posts by creation date
{ $skip: skip }, // Skip for pagination
{ $limit: limit }, // Limit for pagination
{
$lookup: {
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'users'
}
},
{
$unwind: {
path: '$users',
preserveNullAndEmptyArrays: true
}
},
{
$project: {
content: 1,
likes: 1,
comments_count: 1,
attachments: 1,
createdAt: 1,
updatedAt: 1,
fullname: { $concat: ['$users.first_name', " ", '$users.last_name'] }
}
}
]
}
}
]);
const totalDocuments = result[0].totalCount[0]?.totalDocuments || 0;
const posts = result[0].paginatedData;
// Send Response with total document count and paginated posts
return successResponse(res, 200, {
posts,
totalDocuments,
currentPage: page,
pageSize: limit,
message:'Post Fetched Successfully!!'
});
} catch (error) {
return errorResponse(res, 500, `Internal Server Error: ${error.message}`);
}
}
1st way to get count >>>>>>>>>>>>
const getPosts = async (req, res) => {
const { filter, pageNumber = 1, pageSize = 10 } = req.query;
try {
// Convert pageNumber and pageSize to integers
const page = parseInt(pageNumber);
const limit = parseInt(pageSize);
const skip = (page - 1) * limit;
// Match content using regex for partial matching if a filter is provided
const matchStage = filter
? { content: { $regex: filter, $options: 'i' } }
: {};
// Get total document count with filter
const totalDocuments = await Post.countDocuments(matchStage);
// Get Posts with the filter, pagination, and user details
const posts = await Post.aggregate([
{
$match: matchStage
},
{
$lookup: {
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'users'
}
},
{
$unwind: {
path: '$users',
preserveNullAndEmptyArrays: true
}
},
{
$sort: {
createdAt: -1
}
},
{
$skip: skip
},
{
$limit: limit
},
{
$project: {
content: 1,
likes: 1,
comments_count: 1,
attachments: 1,
createdAt: 1,
updatedAt: 1,
fullname: { $concat: ['$users.first_name', " ", '$users.last_name'] },
}
}
]);
// Send Response with total document count and paginated posts
return successResponse(res, 200, {
posts,
totalDocuments,
currentPage: page,
pageSize: limit,
message:'Post Fetched Successfully!!'
});
} catch (error) {
return errorResponse(res, 500, `Internal Server Error: ${error.message}`);
}
}