14

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?

2 Answers 2

34
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);
});
Sign up to request clarification or add additional context in comments.

Comments

4

Instead of using

{options:{limit:2}}

Use like

{perDocumentLimit:2}

Its works for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.