title: Node.js + Mongoose consume all memory on inserts
body: <p>I need to fill Mongo collection with about 80 million of records like</p>
  
  <pre><code>//Example
  {
      "_id" : "4gtvCPATZ",
      "isActivated" : false
  }
  </code></pre>
  
  <p>Where _id is randomly generated. I do this using Node.js Express and Mongoose:</p>
  
  <pre><code>app.get('/api/codes', function(req, res) {
      for (var i = 0; i &lt; 100000; i++) {
          var code = new Code({
              _id: randomStr(9),
              isActivated: 0
          });
          code.save();
          code = null;        
      }
      res.render('index');
  });
  
  function randomStr(m) {
      var m = m || 9;
      s = '', r = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz123456789';
      for (var i = 0; i &lt; m; i++) {
          s += r.charAt(Math.floor(Math.random() * r.length));
      }
      return s;
  };
  </code></pre>
  
  <p>On 'index' jade layout has JS code that reloads the page to generate next 100000 records:</p>
  
  <pre><code>script(type='text/javascript').
    location.href='http://localhost:3000/api/codes'
  </code></pre>
  
  <p>node.js process starts to consume memory and after 4-5 page reloads hangs up having 1GB of memory.</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>UPDATE</strong>:
  Considering the robertklep's comment I've updated the code and it works properly:</p>
  
  <pre><code>app.get('/api/codes', function(req, res) {
  var count = 0;
  async.whilst(
      function () { return count &lt; 100000; },
      function (callback) {
          count++;
          var code = new Code({
              _id: randomStr(9),
              isActivated: 0
          });     
          code.save(function(){
              callback();             
          });
      },
      function (err) {
          res.render('index');
      }
  );
  });
  </code></pre>
  
  <p><strong>UPDATE 2</strong>: I've tested the freakish's advice and made my app work faster:</p>
  
  <pre><code>var MongoDB = require("mongodb");
  app.get('/api/codes', function(req, res) {
    var MongoClient = require('mongodb').MongoClient
      , format = require('util').format;
  
    MongoClient.connect('mongodb://127.0.0.1:27017/code', function(err, db) {
      var collection = db.collection('codes');
  
      var count = 0;
      async.whilst(
          function () { return count &lt; 10; },
          function (callback) {
              count++;
              var docs = [];
              for (var i = 0; i &lt; 100000; i++) {
                  docs.push({
                      _id: randomStr(9),
                      isActivated: 0
                  });
              }
              collection.insert(docs, function(err, docs) {
                  callback();
              });
          },
          function (err) {
              res.render('index');
          }
      );
  
    })
  }); 
  </code></pre>
  
  <p>Now it writes about 1M records in 60-70 seconds.</p>
  
  <p>Thanks!</p>