How to implement pagination and total with mongoose
Probably you could do something like this:
Only if current page index is 0 default and pagination starts with next page, that is 1, your could do .skip(page * limit) make sure .skip() and .limit() gets Number.
router.get("/booksquery", (req, res) => {
var page = parseInt(req.query.page) || 0; //for next page pass 1 here
var limit = parseInt(req.query.limit) || 3;
var query = {};
Book.find(query)
.sort({ update_at: -1 })
.skip(page * limit) //Notice here
.limit(limit)
.exec((err, doc) => {
if (err) {
return res.json(err);
}
Book.countDocuments(query).exec((count_error, count) => {
if (err) {
return res.json(count_error);
}
return res.json({
total: count,
page: page,
pageSize: doc.length,
books: doc
});
});
});
});
from above you will get response like below:
{
books: [
{
_id: 1,
title: "达·芬奇密码 ",
author: "[美] 丹·布朗"
},
{
_id: 2,
title: "梦里花落知多少",
author: "郭敬明"
},
{
_id: 3,
title: "红楼梦",
author: "[清] 曹雪芹"
}
],
total: 3,
page: 0,
pageSize: 3
}
if you use any condition and over that you want to make query and get result and total on that basics. do it inside var query ={}
.
and the same query will be used for .count()
also.
so you can get total
count on the basics of that condition.