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

100
Views
MongoDB loading +3000 entries data pagination

I have to serve an HTML tables with more than 3000 results, getting MachineID, Username, Data from my MongoDB. BUT, i'm having a problem when im rendering the data to datatables.

The Data and MachineID comes from model/table Logs, and the Username comes from User. The relation between them will be user_id from Logs and the _id object from User.

As you can see on my query, im trying to compare pc.user_id with the object _id of parent table, to get the username of that process.

My query:

Pincode.find({}).exec(async (err: any, pincodes: any) => { 
        for (var pc of pincodes){
            var uid = pc.user_id;
            var res = await User.findById({ _id:ObjectId(uid) },'username').exec();
            if(res!= null){
                pc.user_id = res.username
            }else{
                pc.user_id = "Unknown"
            }
        }

But this is slowing SO much my query because im looping trhougt all +3000 documents.

Thanks for your answers,

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try this:

let getData = async () => {
  try {
    let pincodeData = await Pincode.find({}).exec()
    if (pincodeData.length < 1) {
      return res.send({"error": true, "message": "No data found"});
    }
    await Promise.all(
      pincodeData.map(async (x) => {
        let getUserData = await User.findOne(
          { _id: ObjectId(x.user_id) },
          { _id: 0, username: 1}
        ).exec();
        x.user_id: 'Unknown';
        if (getUserData) {
          x.user_id: getUserData.username;
        }
      })
    )
    return res.send({ "error": false, "message": "Data found", "data": pincodeData });
  } catch (error) {
    return res.send({ "error": true, "message": "No data found" });
  }
}
getData()
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!