Mongoose - RangeError:超过最大调用堆栈大小 [英] Mongoose - RangeError: Maximum Call Stack Size Exceeded

查看:67
本文介绍了Mongoose - RangeError:超过最大调用堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文档批量插入 MongoDB(因此绕过 Mongoose 并使用本机驱动程序,因为 Mongoose 不支持批量插入文档数组).我这样做的原因是为了提高写作速度.

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.

我在下面的代码中的 console.log(err) 收到错误RangeError: Maximum Call Stack Size Exceeded":

I am receiving the error "RangeError: Maximum Call Stack Size Exceeded" at console.log(err) in the code below:

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();
      }
  });
} 

非常类似于:https://stackoverflow.com/questions/24356859/mongoose-maximum-call-stack-size-exceeded

也许这与 Mongoose 返回的响应数组的格式有关,这意味着我不能直接使用 MongoDB 本机插入?我在每个响应上都尝试过 .toJSON() 但没有成功.

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.

即使数据量非常少,我仍然会收到错误消息,但循环遍历并分别调用每个文档上的 Mongoose 保存工作正常.

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.

我认为这与这个问题有关:http://howtosjava.blogspot.com.au/2012/05/nodejs-mongoose-rangeerror-maximum-call.html

I think it is related to this issue: http://howtosjava.blogspot.com.au/2012/05/nodejs-mongoose-rangeerror-maximum-call.html

我的响应模式是:

var ResponseSchema = new Schema({
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  randomUUID: String,
  status: String,
  submitted: Date,
  initialEmailId: String,
  survey: String,
  answers: [AnswerSchema]
}); 

因此,答案是响应中的子文档.不知道如何修复它....

So, answers are a sub-document within responses. Not sure how to fix it though....

推荐答案

我遇到了同样的问题,我开始挖掘 mongoose 源代码(版本 3.8.14).最终它让我在

I was having this same issue and I started digging through the mongoose source code (version 3.8.14). Eventually it led me to this line within

  • mongoose/node_modules/mongodb/lib/mongodb/collection/core.js -> insert(...) -> insertWithWriteCommands(...) ->
  • mongoose/node_modules/mongodb/lib/mongodb/collection/batch/ordered.js -> bulk.insert(docs[i]) -> addToOperationsList(...) -> bson.calculateObjectSize(document, false);

  • mongoose/node_modules/mongodb/lib/mongodb/collection/core.js -> insert(...) -> insertWithWriteCommands(...) ->
  • mongoose/node_modules/mongodb/lib/mongodb/collection/batch/ordered.js -> bulk.insert(docs[i]) -> addToOperationsList(...) -> bson.calculateObjectSize(document, false);

var bsonSize = bson.calculateObjectSize(document, false);

var bsonSize = bson.calculateObjectSize(document, false);

显然,这调用了 BSON.calculateObjectSize,后者调用了 calculateObjectSize,然后无限递归.我无法深入了解导致它的原因,但认为它可能与 mongoose 包装器绑定到 Schema 的函数有关.由于我将原始数据插入 mongoDB,因此一旦我决定将 mongoose 中的批量插入更改为标准 javascript 对象,问题就消失了,批量插入正确发生.你也许可以做类似的事情.

Apparently, this calls BSON.calculateObjectSize, which calls calculateObjectSize which then infinitely recurses. I wasn't able to dig that far in to what caused it, but figured that it may have something to do with the mongoose wrapper binding functions to the Schema. Since I was inserting raw data into mongoDB, once I decided to change the bulk insert in mongoose to a standard javascript object, the problem went away and bulk inserts happened correctly. You might be able to do something similar.

基本上,我的代码来自

//EDIT: mongoose.model needs lowercase 'm' for getter method

var myModel = mongoose.model('MyCollection');
var toInsert = myModel();
var array = [toInsert];
myModel.collection.insert(array, {}, function(err, docs) {});

//EDIT: mongoose.model needs lowercase 'm' for getter method

var myModel = mongoose.model('MyCollection');
var toInsert = { //stuff in here 
   name: 'john',
   date: new Date()
};
var array = [toInsert];
myModel.collection.insert(array, {}, function(err, docs) {});

这篇关于Mongoose - RangeError:超过最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆