当我在另一个参考模型中使用参考模型时,MongoDB仅显示ID

时间:2019-07-11 06:33:08

标签: mongodb mongoose schema

我使用mongoose。我试图在

中添加模型

ProductController.js:

const student = require('../models/studentModel');.populate('student_list')

但我仍然得到相同的结果。

productModel.js

{
    lectureProductId: { 
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'Lecture', 
    } 
}

lectureModel.js

{ 
    task_list: [{ 
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'task' 
    }], 
    student_list: [{ 
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'student' 
    }], 
    teacher: { 
        type: mongoose.Schema.Types.ObjectId, 
        ref: 'teacher', required: true, 
    },
}

productController.js

productModel.find() 
    .populate('lectureProductId') 
    .populate('taskProductId') 
    .then(product => { 
        res.json({ 
            status: 'success', 
            message: 'products retrieved successfully', 
            data: product 
        }); 
    })

lectureController.js

lectureModel.find() 
    .populate('teacher') 
    .populate('task_list') 
    .populate('student_list') 
    .then(lecture => { 
        res.json({ 
            status: 'success', 
            message: 'Lectures retrieved successfully', 
            data: lecture, 
        }); 
    })

当我发送请求时,我看到:

"lectureProductId": { 
    "keywords": [ "MATLAB", "BİSECTİON", "NEWTON-RAHSON" ], 
    "task_list": [ "5d26d617454d23000665421c", "5d26d617454d230006654217", "5d26d617454d23000665421b", "5d26d617454d23000665421a", "5d26d617454d230006654218" ], 
    "student_list": [ "5d26d615454d230006654206", "5d26d615454d230006654207", "5d26d615454d230006654208", "5d26d615454d23000665420c", "5d26d615454d23000665420d", "5d26d616454d23000665420e", "5d26d616454d23000665420f" ], 
    "_id": "5d26d617454d230006654221", 
    "name": "Numerical Analysis",
    "programmingLanguages": "MATLAB", 
    "description": "MATLAB for easy level", 
    "teacher": "5d26d616454d230006654215", "__v": 0 
}

如何查看student_list和其他字段?

"student_list": [ 
    "5d26d615454d230006654206", 
    "5d26d615454d230006654207", 
    "5d26d615454d230006654208", 
    "5d26d615454d23000665420c", 
    "5d26d615454d23000665420d", 
    "5d26d616454d23000665420e", 
    "5d26d616454d23000665420f" 
]

1 个答案:

答案 0 :(得分:0)

productModel
    .find()
    .populate({
      path: 'lectureProductId',
      model: 'lecture',
      populate: { path: 'task_list' },
    })

这是我的问题的答案。

productModel
    .find()
    .populate({
      path: 'id with ref',
      model: 'childs model',
      populate: { path: 'id with ref in childs model' },
    })