mongodb find all slow

Viewed 25

I'm new with nodejs and mongodb. I have a simple request which will return 800 entities without any where statements. Just find all. or explain(); Very slow responder.....

Is there a more better way to do collection.find().lean() ?

const Jobs = require("../model/Jobs.mongo");

const saveData = async (title, link, location, idJob, companyName) => {
  const found = await Jobs.findOne({ idJob: idJob });
  if (!found) {
    try {
      const job = new Jobs({
        title: title,
        link: link,
        location: location,
        idJob: idJob,
        companyName: companyName,
      });
      await job.save();
      console.log(job);
    } catch (e) {
      console.log(e);
    }
  } else {
    console.log(`${title} ***** is already in the data with id ***** ${idJob}`);
  }
};

const getAllJobs = async (req, res) => {
  const jobs = await Jobs.find({}).sort({ createdAt: "desc" }).lean();
  res.status(200).json({ jobs, count: jobs.length });
};

const getJobByCompany = async (req, res) => {
  const {
    params: { companyName: companyName },
  } = req;

  const job = await Jobs.find({
    companyName: companyName,
  });
  if (!job) {
    res.status(404).json({});
  }
  res.status(200).json({ job, count: job.length });
};

module.exports = {
  saveData,
  getAllJobs,
  getJobByCompany,
};

1 Answers

If you are facing this issue for a while try to check you internet connection.

You can also take a look how much is the data that you want to receive.

It can be just from a internet speed drop, let me know what is the result :)

Related