title: Mongodb.connect does not execute callback function
body: <p><strong>Im trying to connect to my Mongodb and insert some documents if they are not already in the db. It works fine with the first inserts but in the function existInDatabase it sometimes does not execute the callback function.</strong>  </p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient;
  var mongoData = require('./mongoData');
  var exports = module.exports = {};
  var dbName = 'checklist';
  
  MongoClient.connect(mongoData.ConString, {
    useNewUrlParser: true
  }, function(err, db) {
    if (err) throw err;
    for (var key in mongoData.Customers) {
      if (!existsInDatabase(mongoData.Customers[key], 'Customers')) {
        db.db(dbName).collection('Customers').insertOne(mongoData.Customers[key], function(err, res) {
          if (err) throw err;
          console.log('1 document inserted');
          db.close();
        });
      }
    }
    for (var key in mongoData.Categorys) {
      if (!existsInDatabase(mongoData.Customers[key], 'Customers')) {
        db.db(dbName).collection('Categorys').insertOne(mongoData.Categorys[key], function(err, res) {
          if (err) throw err;
          console.log('1 document inserted');
          db.close();
        });
      }
    }
  });
  
  function existsInDatabase(obj, collection) {
    var result = false;
    MongoClient.connect(mongoData.ConString, {
      useNewUrlParser: true
    }, function(err, db) {
      db.db(dbName).collection(collection).find({}).forEach(function(doc) {
        if (doc.id == obj.id) {
          result = true;
        }
      }, function(err) {
        console.log(err);
      });
    });
    return result;
  }
  </code></pre>