1
我有两个模式:猫鼬 - 如何限制填充()级别的深度?
var categorySchema = mongoose.Schema({
title: String,
blogs: [{ type: ObjectId, ref: 'Blog' }]
})
var blogSchema = mongoose.Schema({
title: String,
description: String,
category: [{ type: ObjectId, ref: 'Category' }],
created: {type: Number, default: new Date().getTime()}
})
var Category = mongoose.model('Category', categorySchema)
var Blog = mongoose.model('Blog', blogSchema)
它们交叉指:
- 博客对象包含
Category
对象(裁判)的阵列能够得到这一切的博客相关的类别。 - 类别对象包含一个
Blog
对象(参考)的数组,以便能够获取此类别的所有博客。
问题是当我试图获得某个博客。我需要填充类阵列来获得它的标题:
Blog
.findOne({_id: _._id})
.populate('category')
.exec(function (err, __) {
callback(err, __);
})
我得到...
{ title: 'My Blog',
description: 'description',,
_id: 51cb6bd845ba145e02000001,
__v: 0,
created: 1372285906662,
category:
[ { __v: 0,
_id: 51cb5ed6fd63867905000002,
priority: 3,
title: 'Music',
blogs: [Object] } ],
}
是的,我得到类别的冠军,但我也得在博客某些对象 - 他们也人口稠密。但是博客对象可能包含很多子对象(帖子)并且我们还记得category
字段。那么,类别字段中的所有对象都将递归填充,并且由于我在Blog和Category之间存在交叉链接,它将填充到循环中?
如何限制人口水平?我不希望Blog/category [n]/blogs: []
被填充,只能直接输入category
字段,如title
。谢谢。