0
I have a question about performance using Mongoose.
Suppose the following models:
const Disciplina = new Schema({
titulo: {
type: String,
}
})
const Assunto = new Schema({
disciplina: {
type: ObjectId,
ref: 'Disciplina',
}
})
What is the best way to seek the subjects and titles of the disciplines of each subject?
1.
Assunto.find({}).populate('disciplina')
const assuntos = Assunto.find({})
const disciplinasIds = assuntos.map(it => it.disciplina)
const disciplinas = Disciplina
.find({ _id: {$in: disciplinasIds}})
and then manually insert the discipline on my return
It is obvious that the first way is simpler and clean, but my doubt is regarding the performance of .populate
. If I have two "Subjects" with the same discipline id, two will be made finds
? How does it work?
Thank you!