title: Correct way to insert many records into Mongodb with Node.js
body: <p>I was wondering what is the correct way to do bulk inserts into Mongodb (although could be any other database) with Node.js</p>
  
  <p>I have written the following code as an example, although I believe it is floored as db.close() may be run before all the asynchronous collection.insert calls have completed. </p>
  
  <pre><code>MongoClient.connect('mongodb://127.0.0.1:27017/test', function (err, db) {
      var i, collection;
      if (err) {
          throw err;
      }
      collection = db.collection('entries');
      for (i = 0; i &lt; entries.length; i++) {
          collection.insert(entries[i].entry);
      }
      db.close();
  });
  </code></pre>