如何按类别查找博客? 我的架构:
var categorySchema = new Schema({
slug: { type: String, required: true, unique: true },
name: { type: String, required: true},
});
var blogSchema = new Schema({
slug: { type: String, required: true, unique: true },
name: { type: String, required: true },
category: { type: Schema.Types.ObjectId, ref: 'Category' },
content: String
});
模型
var Category = mongoose.model('category', categorySchema);
var Blog = mongoose.model('blog', blogSchema);
我试试:
Blog.find()
.populate('category', 'slug name', {slug: req.params.slug })
.exec(function(error, data){
if(error)
res.json({success: 0});
else
res.json({success: 1, data: data});
});
但结果:
数据:[{ ... 类别:{ slu ::"搞笑", 名字:"搞笑" } ... { slu ::" some-thing-2", 名称:" Some Thing 2", 类别:null,< - HERE 内容:"我的内容2" }]
类别2为空。