async function insertManyCallback(collectionName, query, options, callback) {
try {
await MongoClient.connect(
dburl,
defaultOptions,
function(err, client) {
if (err) logger.error(err);
if (!err) {
client
.db(dbName)
.collection(collectionName)
.insertMany(query, options, function(err, res) => {
client.close();
callback();
});
}
}
);
} catch (err) {
logger.error(err);
}
return true;
}
I’m inserting approximately 65K documents for each bulk write operation, and I understand it will be slow. My problem stems from me querying the MongoDB collection for count documents. I can see the number go up and up until it has “visibly” finished inserting all the documents. Then, the insertMany just freezes there for a minute without any visible changes to the collection before calling my callback function.
Does anyone know why it freezes for a minute and anything I can do about it?
I also tried .then instead of function(err, res) to try and bypass it returning a result; the same outcome in terms of performance.