And*_*rew 28 javascript mongoose mongodb node.js
我正在尝试将文档批量插入MongoDB(因此绕过Mongoose并使用本机驱动程序,因为Mongoose不支持批量插入文档数组).我这样做的原因是为了提高写作速度.
我在下面的代码中收到console.log(错误)中的错误"RangeError:Maximum Call Stack Size Exceeded":
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();
}
});
}
Run Code Online (Sandbox Code Playgroud)
类似于:https://stackoverflow.com/questions/24356859/mongoose-maximum-call-stack-size-exceeded
也许这是关于Mongoose返回的响应数组的格式,这意味着我不能直接使用MongoDB插入?我已经尝试过每次响应的.toJSON(),但没有运气.
我仍然得到错误,即使只有非常少量的数据,但循环并调用Mongoose保存在每个文档单独工作正常.
编辑:我认为这与此问题有关: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]
});
Run Code Online (Sandbox Code Playgroud)
因此,答案是答复中的子文件.不知道怎么解决它虽然....
roc*_*egg 30
我遇到了同样的问题,我开始挖掘猫鼬源代码(版本3.8.14).最终它引导我进入这条线
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);
显然,这会调用BSON.calculateObjectSize,它调用calculateObjectSize然后无限递归.我无法深入了解导致它的原因,但认为它可能与模仿的mongoose包装器绑定功能有关.由于我将原始数据插入到mongoDB中,一旦我决定将mongoose中的批量插入更改为标准javascript对象,问题就消失了,批量插入正确发生.你也许可以做类似的事情.
基本上,我的代码来自
//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) {});
Run Code Online (Sandbox Code Playgroud)
至
//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) {});
Run Code Online (Sandbox Code Playgroud)
小智 18
确认,但不是错误.Model.collection.insert()
绕过Mongoose,所以你告诉节点驱动程序插入一个包含猫鼬内部组件等的对象$__
.堆栈溢出可能是因为bson试图计算间接引用自身的对象的大小.
长话短说,使用Document.toObject()
,这就是它的用途:http://mongoosejs.com/docs/api.html#document_Document-toObject
Response.find({}).exec(function(err, responses) {
if (err) {
return callback(err);
}
if (true) {
var toInsert = [];
responses.forEach(function(response) {
console.log("Filling response: " + response._id);
response.answers = [];
[{ name: 'test' }].forEach(function(ans) {
response.answers.push(ans);
});
toInsert.push(response.toObject());
});
Response.collection.insert(toInsert, function(err, responsesResult) {
console.log(err);
});
} else {
callback();
}
});
Run Code Online (Sandbox Code Playgroud)
此外,即使您修复堆栈溢出,您指定的代码也将无法工作.由于您正在尝试insert()
已存在于数据库中的文档,因此所有插入都会因_id
冲突而失败.你最好只使用一个stream()来一次读取一个结果,然后再将save()
它们返回到db中.
归档时间: |
|
查看次数: |
19503 次 |
最近记录: |