populate with mongoose pagination
Solution 1:
A populate have following things
Post.find({})
.populate([
// here array is for our memory.
// because may need to populate multiple things
{
path: 'user',
select: 'name',
model:'User',
options: {
sort:{ },
skip: 5,
limit : 10
},
match:{
// filter result in case of multiple result in populate
// may not useful in this case
}
}
])
.exec((err, results)=>{
console.log(err, results)
})
Solution 2:
If you want to use mongoose-paginate, You can do the following
var query = {};
var options = {
sort: { date: -1 },
populate: 'users',
lean: true,
offset: offset,
limit: 10
};
Post.paginate({}, options, (err, result) => {
//....
})