You need to sort your document by a field then use skip & limit aggregation. Also to get the total number of records we can use facet aggregation.
Here is a detailed explanation:
Let's say you have these 8 documents in orders collection.
[
{
"_id": "5e390fc33285e463a0799689",
"customer": "Max",
"total": 10,
"orderDate": "2020-02-04T06:31:31.311Z",
"__v": 0
},
{
"_id": "5e390fd03285e463a079968a",
"customer": "John",
"total": 9.2,
"orderDate": "2020-02-04T06:31:44.190Z",
"__v": 0
},
{
"_id": "5e390fda3285e463a079968b",
"customer": "Smith",
"total": 11.3,
"orderDate": "2020-02-04T06:31:54.248Z",
"__v": 0
},
{
"_id": "5e390fdf3285e463a079968c",
"customer": "Smith",
"total": 12.3,
"orderDate": "2020-02-04T06:31:59.993Z",
"__v": 0
},
{
"_id": "5e390fec3285e463a079968d",
"customer": "Jimmy",
"total": 15.6,
"orderDate": "2020-02-04T06:32:12.336Z",
"__v": 0
},
{
"_id": "5e390ffd3285e463a079968e",
"customer": "Wesley",
"total": 11,
"orderDate": "2020-02-04T06:32:29.670Z",
"__v": 0
},
{
"_id": "5e3910163285e463a079968f",
"customer": "Adam",
"total": 6.1,
"orderDate": "2020-02-04T06:32:54.131Z",
"__v": 0
},
{
"_id": "5e3910213285e463a0799690",
"customer": "Michael",
"total": 7.2,
"orderDate": "2020-02-04T06:33:05.166Z",
"__v": 0
}
]
If we wanted to get these documents in chunks, we can write a sample route like this:
router.get("/orders", async (req, res) => {
const page = req.query.pageIndex ? +req.query.pageIndex : 1;
const limit = req.query.pageSize ? +req.query.pageSize : 10;
const skip = (page - 1) * limit;
const result = await Order.aggregate([
{
$sort: {
orderDate: -1
}
},
{
$facet: {
totalRecords: [{ $count: "total" }],
data: [{ $skip: skip }, { $limit: limit }]
}
}
]);
res.send(result);
});
We send the pageIndex and pageSize parameters in query string like this http://...../orders?pageIndex=1&pageSize=3
When we use pageIndex=1
and pageSize=3
, the result will be like this: (as you see we also return the total number of records so that client can build the pagination numbers)
[
{
"totalRecords": [
{
"total": 8
}
],
"data": [
{
"_id": "5e3910213285e463a0799690",
"customer": "Michael",
"total": 7.2,
"orderDate": "2020-02-04T06:33:05.166Z",
"__v": 0
},
{
"_id": "5e3910163285e463a079968f",
"customer": "Adam",
"total": 6.1,
"orderDate": "2020-02-04T06:32:54.131Z",
"__v": 0
},
{
"_id": "5e390ffd3285e463a079968e",
"customer": "Wesley",
"total": 11,
"orderDate": "2020-02-04T06:32:29.670Z",
"__v": 0
}
]
}
]
When we use pageIndex=2
and pageSize=3
, the result will be like this:
[
{
"totalRecords": [
{
"total": 8
}
],
"data": [
{
"_id": "5e390fec3285e463a079968d",
"customer": "Jimmy",
"total": 15.6,
"orderDate": "2020-02-04T06:32:12.336Z",
"__v": 0
},
{
"_id": "5e390fdf3285e463a079968c",
"customer": "Smith",
"total": 12.3,
"orderDate": "2020-02-04T06:31:59.993Z",
"__v": 0
},
{
"_id": "5e390fda3285e463a079968b",
"customer": "Smith",
"total": 11.3,
"orderDate": "2020-02-04T06:31:54.248Z",
"__v": 0
}
]
}
]
When we use pageIndex=3
and pageSize=3
, the result will be like this:
[
{
"totalRecords": [
{
"total": 8
}
],
"data": [
{
"_id": "5e390fd03285e463a079968a",
"customer": "John",
"total": 9.2,
"orderDate": "2020-02-04T06:31:44.190Z",
"__v": 0
},
{
"_id": "5e390fc33285e463a0799689",
"customer": "Max",
"total": 10,
"orderDate": "2020-02-04T06:31:31.311Z",
"__v": 0
}
]
}
]
For your case, you need to send 10 as pageSize value.