title: Mongoose - RangeError: Maximum Call Stack Size Exceeded
body: <p>I am trying to bulk insert documents into MongoDB (so bypassing Mongoose and using the native driver instead as Mongoose doesn't support bulk insert of an array of documents).  The reason I'm doing this is to improve the speed of writing.</p>
  
  <p>I am receiving the error "RangeError: Maximum Call Stack Size Exceeded" at console.log(err) in the code below: </p>
  
  <pre><code>function _fillResponses(globalSurvey, optionsToSelectRegular, optionsToSelectPiped, responseIds, callback) {
    Response.find({'_id': {$in: responseIds}}).exec(function(err, responses) {
      if (err) { return callback(err); }
  
      if (globalSurvey.questions.length) {
        responses.forEach(function(response) {
          console.log("Filling response: " + response._id);
          response.answers = [];
          globalAnswers = {};
          globalSurvey.questions.forEach(function(question) {
            ans = _getAnswer(question, optionsToSelectRegular, optionsToSelectPiped, response);
            globalAnswers[question._id] = ans;
            response.answers.push(ans);
          });
        });
        Response.collection.insert(responses, function(err, responsesResult) {
          console.log(err);
          callback()
        });
      } else {
          callback();
        }
    });
  } 
  </code></pre>
  
  <p>So similar to: <a href="https://stackoverflow.com/questions/24356859/mongoose-maximum-call-stack-size-exceeded">https://stackoverflow.com/questions/24356859/mongoose-maximum-call-stack-size-exceeded</a></p>
  
  <p>Perhaps it's something about the format of the responses array that Mongoose returns that means I can't directly insert using MongoDB natively?  I've tried .toJSON() on each response but no luck.</p>
  
  <p>I still get the error even with a very small amount of data but looping through and calling the Mongoose save on each document individually works fine.</p>
  
  <p>EDIT: I think it is related to this issue: <a href="http://howtosjava.blogspot.com.au/2012/05/nodejs-mongoose-rangeerror-maximum-call.html" rel="noreferrer">http://howtosjava.blogspot.com.au/2012/05/nodejs-mongoose-rangeerror-maximum-call.html</a></p>
  
  <p>My schema for responses is: </p>
  
  <pre><code>var ResponseSchema = new Schema({
    user: {
      type: Schema.ObjectId,
      ref: 'User'
    },
    randomUUID: String,
    status: String,
    submitted: Date,
    initialEmailId: String,
    survey: String,
    answers: [AnswerSchema]
  }); 
  </code></pre>
  
  <p>So, answers are a sub-document within responses.  Not sure how to fix it though....</p>