我不确定如何在以下示例中填充questions
集合中的examBoard
字段(我的目的是使我的示例合理复杂,因此我可以正确理解它是如何工作的)
examBoard架构:
var topicSchema = new mongoose.Schema({
name: String,
questions:[
{
type:mongoose.Schema.Types.ObjectId,
ref:"question"
}
],
});
var moduleSchema = new mongoose.Schema({
name: String,
topics: [topicSchema]
});
var examBoardSchema = new mongoose.Schema({
name: String,
modules: [moduleSchema]
});
module.exports = mongoose.model("examBoard", examBoardSchema);
问题架构:
var partSchema = new mongoose.Schema({
mark: Number,
content: String
});
var questionSchema = new mongoose.Schema({
content: String,
mark:Number,
methods:[[partSchema]]
});
module.exports = mongoose.model("question", questionSchema);
我认为我应该这样做:
examBoard.find()
.populate
({
path:"modules.topics.questions",
model:"question"
})
.exec(function(err,exam)
{
if(err)
{
console.log("Failed to populate");
}
else
{
console.log("exam[0].modules[0].topcis[0].questions\n"+exam.modules[0].topcis[0].questions);
}
});
答案 0 :(得分:1)
试试这个:
Exam
.find()
.exec()
.then((exams) => {
// Populate questions
Exam
.populate(exams, {
path: 'modules.topics.questions',
model: 'question'
})
.then((populatedExams) => {
// Do something with populated exams
});
});