Nodejs: Mongoose (mongodb) select and save 3000 items take a long time

Viewed 18

This is my code:

for (const element of data) {
        let find = await Coin.findOne({name: element.coin, network: element.network});
        if (!find) {
            new Coin({
                _id: new mongoose.Types.ObjectId(),
                name: element.coin,
                title: element.name,
                network: element.network,
                contract: element.contract
            }).save()
        }
    }

But this code take time about 2 minutes for 3000 items!

1 Answers

you are trying to insert data using loop and also your making new query every time to collection , which makes the process time-consuming as it involves opening connection and logging, creating indexes in background . so first we need to optimise the process at our end, let mongo their own method to bulk data using its own method

exports.bulkUpdateInsert = (data) => {
  return new Promise((resolve, reject) => {
    Tag(Co)
      .bulkWrite(data)
      .then((doc) => {
        resolve(doc);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

Use the above code snippet to make it efficient

Related