如何使.populate排除空值?

时间:2016-10-20 18:07:58

标签: node.js mongodb express mongoose mongodb-query

有没有办法排除等于null的lessons.lesson_id?我从以前的答案中得到以下内容,但它仍然提供无效的课程.lesson_id

newList.add(test.id.ToString());
newList.add(test.timeSlot + test.dateSlot);

我正在尝试阻止使用null的lesson_id显示

module.exports.getClasses = function(id, callback){
    Class.findById(id)
    .populate(
        {path: 'lessons.lesson_id',
        model: 'Lesson', $ne: null}
    ).exec(callback)
}

var classSchema = new Schema({
    title: { type: String, required: true },
    lessons: [{
        _id: false,
        lesson_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Lesson'}
    }],
});

1 个答案:

答案 0 :(得分:0)

So your ClassSchema is a touch off, I think you may find this will work better based on your requirements:

var classSchema = new Schema({
    title: { type: String, required: true },
    lessons: [{type: mongoose.Schema.Types.ObjectId, ref: 'Lesson'}],
});