Imagining the following structure (illustrative example only):
Collection: Schools
[
{
"_id":"abc123",
"nomeEscola":"escola1",
"turmas":{
"_id":"ccc333",
"nomeTurma":"Turma1"
}
},
{
"_id":"abc122",
"nomeEscola":"escola2",
"turmas":{
"_id":"ddd444",
"nomeTurma":"Turma1"
}
}
]
Now, let's say I'm going to create a new collection of students, and I want her schema in mongoose to have ID
TURMA
and no ESCOLA
StudentModel:
let mongoose = require('mongoose');
Schema = mongoose.Schema;
let alunoSchema = new Schema({
_id : {type: Schema.Types.ObjectId},
nomeAluno : {type: String},
idTurma: {type: Schema.Types.ObjectId, ref : 'Escolas'},
}, {collection : 'Alunos'});
var aluno = mongoose.model('Aluno', alunoSchema);
module.exports = aluno;
How do I ensure that
idTurma
will reference the class id and not the school id?