how can i exit nodejs script after mongo db write
<p>I have 400,000 lines to enter and I need to break it up.  Unfortunately I cannot get this script to exit until it's completed everything.  It invariably runs out of memory of course.  I thought setting a value at .on('end',function()  would be useful but I can't see that value once the .on data has completed.</p>

<pre><code>'use strict';
var mongoose = require('mongoose');
var fs = require('fs');
var parse = require('csv-parse');
var Schema = mongoose.Schema;
var done;

mongoose.connect('mongodb://127.0.0.1:27017/auth');

var userSchema = new mongoose.Schema({
  username: {
    type: String,
    unique: true
  },
  password: String,
  email: {
    type: String,
    unique: true
  },
  isActive: String,
  roles: {
    account: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Account'
    }
  },
  timeCreated: {
    type: Date,
    default: Date.now
  },
  search: [String]
});

var accountSchema = new mongoose.Schema({
  user: {
    id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    },
    name: {
      type: String,
      default: ''
    }
  },
  isVerified: {
    type: String,
    default: ''
  },
  verificationToken: {
    type: String,
    default: ''
  },
  name: {
    first: {
      type: String,
      default: ''
    },
    middle: {
      type: String,
      default: ''
    },
    last: {
      type: String,
      default: ''
    },
    full: {
      type: String,
      default: ''
    }
  },
  company: {
    type: String,
    default: ''
  },
  phone: {
    type: String,
    default: ''
  },
  zip: {
    type: String,
    default: ''
  },
  memberid: {
    type: String,
    default: ''
  },
  status: {
    id: {
      type: String,
      ref: 'Status'
    },
    name: {
      type: String,
      default: ''
    },
    userCreated: {
      id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
      },
      name: {
        type: String,
        default: ''
      },
      time: {
        type: Date,
        default: Date.now
      }
    }
  },
  userCreated: {
    id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    },
    name: {
      type: String,
      default: ''
    },
    time: {
      type: Date,
      default: Date.now
    }
  },
  search: [String]
});

var User = mongoose.model('User', userSchema);
var Account = mongoose.model('Account', accountSchema);

fs.createReadStream('./ipart')
  .pipe(parse({
    delimiter: ','
  }))
  .on("data-invalid", function(data) {})
  .on('data', function(csvrow) {
    var u = {
      isActive: 'yes',
      username: csvrow[0],
      email: csvrow[0],
      search: [
        csvrow[1] + ' ' + csvrow[2],
        csvrow[0],
      ]
    };

    User.create(u, function(err, createdUser) {
      if (err) {
        console.log(err);
        return;
      }
      var user = createdUser;
      var displayName = csvrow[1] + ' ' + csvrow[2] || '';
      var nameParts = displayName.split(' ');
      var acct = {
        isVerified: 'no',
        'name.first': nameParts[0],
        'name.last': nameParts[1] || '',
        'name.full': displayName,
        user: {
          id: user._id,
          name: user.username
        },
        search: [
          nameParts[0],
          nameParts[1] || ''
        ]
      };

      Account.create(acct, function(err, account) {
        if (err) {
          return workflow.emit('exception', err);
        }
        var fieldstoset = {
          roles: {
            account: account._id
          }
        };
        User.findByIdAndUpdate(account.user.id, fieldstoset, function(err, user) {
          if (err) throw err;
        });
      });
    });
  })
  .on('end', function() {
    console.log('complete');
  });
</code></pre>