I have two schema say city and blogs. I want to create a blog page with pagination. So I am populating blogs with reference to city name. Say if I dont have a blog for a city then it should not be returned. Here is my query to get the blogs.
City.find().skip(req.params.pageIndex*2).limit(2).sort('-created').populate('articles').exec(function(err, cities) {
res.jsonp(cities)
}
If I use the above query. I get the cities with no blogs as well. So it results in an empty row in the view. I don't want that to happen. How to limit the populated field and not return the city without blogs. Any suggestions?
Instead of using
{options:{limit:2}}
Use like
{perDocumentLimit:2}
Its works for me
City.find({}).populate({
path:'Articles',
options: {
limit: 2,
sort: { created: -1},
skip: req.params.pageIndex*2
}
}).exec(function (err, cities) {
if (err) return handleError(res, err);
return res.status(200).json(cities);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With