Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda