【发布时间】:2016-08-06 13:50:58
【问题描述】:
我在猫鼬中有以下架构,
Schema = new Schema({
category = { type: Schema.Types.ObjectId, ref: 'Category' },
subCategory = { type: Schema.Types.ObjectId, ref: 'subCategory' },
subSubCategory = { type: Schema.Types.ObjectId, ref: 'subSubCategory' },
name: String
});
现在我想根据通过req.query传递给控制器的一些参数有条件地填充category、subCategory、subSubCategory
Schema.find(function(err, data) {
if(err) { //handle errors }
if(!data) { //throw 404 }
res.status(200).json(data);
})
.populate('category') //execute only if(req.query.populateCategory == true)
.populate('subCategory') //execute only if(req.query.populateSubCategory == true)
.populate('subSubCategory'); //execute only if(req.query.populateSubSubCategory == true)
如何实现?
【问题讨论】:
标签: node.js express mongoose mean-stack