【发布时间】:2019-04-21 17:56:10
【问题描述】:
我有两个架构,如下所示
Student.js
module.exports = (mongoose) => {
const Schema = mongoose.Schema;
const studentsSchema = new Schema({
name : {
type : String,
required : true
},
roll : {
type : Number,
default : null
},
class : {
type : String,
default : null
}
});
return mongoose.model('students', studentsSchema);
};
Subject.js
module.exports = (mongoose) => {
const Schema = mongoose.Schema;
const subjectSchema = new Schema({
title : {
type : String,
required : true
},
author : {
type : String,
default : null
},
price : {
type : Number,
default : null
},
studentId : {
type : String
}
});
return mongoose.model('subjects', subjectSchema);
};
我需要在 Student 模型上运行 find 查询以获取学生数组。每个学生都将包含一系列他的科目。主题数组的每个索引都将包含主题的完整对象。如下。
[
{
name : "student 1",
roll : 1234,
class : "TEN",
subjects : [
{
title : 'English',
author : 'peter',
price : 210
},
{
title : 'Math',
author : 'Nelson',
price : 222
}
]
}
]
我怎样才能通过使用 refs 来实现它?
【问题讨论】:
标签: node.js express mongoose mongoose-schema mongoose-populate