function getDigitalMigrationJoin(req, res, next) {
DigitalMigrationForm.aggregate([
// Join with user_info table
{
$lookup: {
from: DigitalMigrationFormList.collection.name, // other table name
localField: "_id", // name of users table field
foreignField: "digitalFormId", // name of userinfo table field
as: "forms" // alias for userinfo table
}
},
]).exec(function (err, results) {
console.log(results)
res.send(results)
})
}
i want to add pa gination on this function with limit and page please help me
2
Answers
To add pagination, you cam use the
$skip
and$limit
within theaggregate
method.$skip
skips a specific number of documents;$limit
limits the number of documents passed to the next stage in the pipeline.He is an updated version of your function:
You can do it like this:
Note the use of
$facet
stage, that allows us to return both the total records count, as well as all the document for requested page.