Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
how to use .populate() in javascript

I have a collection called

CR

what I want to do is query CR like so:

let cre = await CR.find({myid: "xxxxxx"})

and i also have a collection called cla, but that one i need to query based off of the results of cre. The results of cre will return a class_id, in which I need to use to query the cla collection to find the _id. At the end of all of this, I want to ideally merge the two, which I believe you can do through .populate(), and then send it to teh front-end as one.;

I have tried this:

 let cre = await cr.find({myid: "xxx"}).populate('loc').populate('desc').populate('dt');

but this isn't working. how can I fix this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It may be due to schemas, but this is how it's clean and simple to use;

 let cre = await cr.find({myid: "xxx"}).populate(['loc','desc','dt']);

about 4 years ago · Juan Pablo Isaza Relatório

0

Firstly, you can take cla collection "_id" in CR collection schema. In schema of CR collection refer to cla model id like this,

const creSchema = mongoose.Schema({
    name: String,
    classnId: { type: mongoose.Types.ObjectId, ref: "Cla" }
});

Then you can populate like,

const cres = await CR.find({}).populate({path:'classnId', select:'columnName'});

Hopefully, this will solve your issue.

Note: There in populating you can give multiple column names by space and if you give a minus before a column name like this (-columnName) then that column will not show when you will call the API.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda