You can use async.mapLimit
to limit the number of parallel requests/connections.
But you're also not closing your connections when you're done with them, which might (also) be the reason why you're running out of connections:
var mongoClient = new MongoClient.connect(uri,function(err,db){
if (!err) {
db.collectionNames(function(error,collections){
// done, close connection
db.close();
// call back with results or error
if (!error){
callback(null, collections);
} else {
callback(error, null);
}
});
} else {
callback(err, null);
}
});
(the code above should also fix your mixing of the err
and error
variables)