Node JS猫鼬填充限制 [英] Node js mongoose populate limit

查看:72
本文介绍了Node JS猫鼬填充限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模式,分别是城市和博客.我想创建一个带有分页的博客页面.因此,我使用城市名称填充博客.说如果我没有一个城市的博客,那么它不应该被退回.这是我查询博客的方法.

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?

推荐答案

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);
});

这篇关于Node JS猫鼬填充限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆