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

查看:707
本文介绍了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上收到错误RangeError:超出最大调用堆栈大小(错误) )在下面的代码中:

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....

推荐答案

我遇到了同样的问题,我开始挖掘猫鼬来源代码(版本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包装器绑定功能有关。由于我将原始数据插入到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天全站免登陆