Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!