"error": { "ok": 0, "code": 5107201, "codeName": "Location5107201", "$clusterTime": { "clusterTime": { "$timestamp": "7292060947630260235" }, "signature": { "hash": "HQW2T+0z31Kf8q6iLViqdu0uYtQ=", "keyId": { "low": 2, "high": 1683641553, "unsigned": false } } }, "operationTime": { "$timestamp": "7292060947630260235" } }
let page = 1;
let limit = 10;
let sort = {};
let skip =(page-1)*limit
//req.query.sort is coming as 'name' or '-name'
if (req.query.sort) {
const key = req.query.sort;
if (req.query.sort[0] === "-") {
sort[`${key.substring(1)}`] = -1;
sort["_id"] = 1;
} else {
sort[`${key}`] = 1;
sort["_id"] = 1;
}
} else {
sort.dateCreated = 1;
}
const reviewsList = await Review.aggregate([
{ $match: filter },
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "user",
},
},
{
$unwind: "$user",
},
{
$lookup: {
from: "products",
localField: "product",
foreignField: "_id",
as: "product",
},
},
{
$unwind: "$product",
},
{
$sort: sort,
},
{ $skip: skip },
{ $limit: limit },
]);
The problem was that the page and limit were String
const reviewsList = await Review.aggregate([ ....... { $skip: +skip }, { $limit: +limit }, ]);