How . populate searches the data

Asked

Viewed 65 times

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!

1 answer

0

I did a bigger search, I used the flag debug from Mongoose and I saw that it basically makes the case 2. under the cloths.

Then you better use the populate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.