I have two schemas just as the following
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);
};
I need to run find query on the Student model to get an array of students. And Every Student will contain an array of his subjects. Every index of subjects array will contain the complete object of subjects. just as following.
[
{
name : "student 1",
roll : 1234,
class : "TEN",
subjects : [
{
title : 'English',
author : 'peter',
price : 210
},
{
title : 'Math',
author : 'Nelson',
price : 222
}
]
}
]
How can i achieve it by using refs ?