节点 js 猫鼬填充限制 [英] Node js mongoose populate limit
本文介绍了节点 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);
});
这篇关于节点 js 猫鼬填充限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文