0

   exports.Country = async (req, res, err) => {

  MongoClient.connect(url, function (err, db) {
    if (err)
      send.json({ message: err.message, statuscode: err.status });
    var dbo = db.db(DATABASE);
    dbo.collection("countries").find({}, { projection: { _id: 0, iso2: 1, name: 1, } }).toArray(function (err, result) {
      if (err) {
        res.json({ message: err.message, statuscode: err.Response })
      };
      res.json({ success: true, statuscode: 200, response: result });
      db.close();
    })
  })
}

Hi everyone it is working fine but i think there should be a better way can someone suggest NOTE:I am doing this crud operations on a collection which does not have any schema i just imported json collection to my database

Krishna
  • 53
  • 5
  • You are retrieving the entire collection in your query. Is that intended? – rickhg12hs Mar 21 '22 at 23:29
  • yes it is and my question is can we do any better way of accessing db and collection whenever we needed? – Krishna Mar 22 '22 at 04:00
  • Time to retrieve docs is composed of IO, storage, and query time (amongst others). Hopefully you can limit the IO time by using an appropriate `find`/`aggregation` query so that only the docs of interest need to be returned. Make MongoDB server do as much of the work as possible. You can look at `explain` results to see how the server is spending it's time, etc. In general, retrieving an entire collection should be avoided - it's just using the DB as storage with no effective query. – rickhg12hs Mar 22 '22 at 04:19

0 Answers0