I am facing a challenge with uploading a substantial data set from my node.js application to a mongodb. The process involves running a for loop to fetch each result. Initially, I established the connection to the MongoDB within every iteration of the loop, but this approach led to crashes.
After moving the connection setup outside the loop, encountering issues with db.close()
. Leaving it inside the loop results in prematurely closing the connection, causing the app to crash with the error:
MongoError: server instance pool was destroyed
However, when attempting to relocate the db.close()
command outside the loop, it gets stuck and fails to exit the loop to proceed with subsequent operations.
I am seeking advice on how to efficiently close the database connection outside the for
loop only upon completion of all iterations.
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydatabase");
//execute SQL query to fetch data
for(result in sqldata) {
myobj = {Number: sqldata[result].Num} //<--- this for loop produces 16000 entries
dbo.collection("customers").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close(); //<--- this is the issue
});
}
});