title: Importing a very large record set into MongoDB using nodejs
body: <p>Before I dive into my question, I wanted to point out that I am doing this partially to get familiarized with node and mongo. I realize there are probably better ways to accomplish my final goal, but what I want to get out of this is a general methodology that might apply to other situations.</p>
  
  <p><strong>The goal:</strong></p>
  
  <p>I have a csv file containing 6+ million geo-ip records. Each record contains 4 fields in total and the file is roughly 180mb.</p>
  
  <p>I want to process this file and insert each record into a MongoDB collection called "Blocks". Each "Block" will have the 4 fields from the csv file. </p>
  
  <p><strong>My current approach</strong></p>
  
  <p>I am using mongoose to create a "Block" model and a ReadStream to process the file line by line. The code I'm using to process the file and extract the records works and I can make it print each record to the console if I want to.</p>
  
  <p>For each record in the file, it calls a function that creates a new Blocks object (using mongoose), populates the fields and saves it.</p>
  
  <p><em>This is the code inside of the function that gets called every time a line is read and parsed. The "rec" variable contains an object representing a single record from the file.</em></p>
  
  <pre><code>block = new Block();
  
  block.ipFrom    = rec.startipnum;
  block.ipTo      = rec.endipnum;
  block.location  = rec.locid;
  
  connections++;
  
  block.save(function(err){
  
      if(err) throw err;
      //console.log('.');
      records_inserted++;
  
      if( --connections == 0 ){
          mongoose.disconnect();
          console.log( records_inserted + ' records inserted' );
      }
  
  });
  </code></pre>
  
  <p><strong>The problem</strong></p>
  
  <p>Since the file is being read asynchronously, more than one line is processed at the same time and reading the file is much faster than MongoDB can write so the whole process stalls at around 282000 records and gets as high up as 5k+ concurrent Mongo connections. It doesn't crash.. it just sits there doing nothing and doesn't seem to recover, nor does the item count in the mongo collection go up any further.</p>
  
  <p>What I'm after here is a general approach to solving this problem. How would I cap the number of concurrent Mongo connections? I would like to take advantage of being able to insert multiple records at the same time, but I'm missing a way to regulate the flow.</p>
  
  <p>Thank you in advance.</p>