我工作的MongoDB 2.6.9和的NodeJS 0.10.37,并根据我刚才的问题MongoDB calculating score from an existing fields and put it in a new field in the same collection和我有一个结构化的答案做这件事从chridam就像这样:批量插入MongoDB中和的NodeJS
var bulkUpdateOps = db.collection1.initializeUnorderedBulkOp(),
cursor = db.collection1.find(), // cursor
counter = 0;
cursor.forEach(function(doc) {
// computations
var c1, c2, c3, c4, Field8;
c1 = 10 + (0.03*doc.Field3);
c2 = (doc.Field2 == 1) ? 1: 0.03;
c3 = 7 - (doc.Field5.match(new RegExp(".", "g")) || []).length;
c4 = (doc.Field2 == 1) ? Math.pow(doc.Field, -0.6) : 1;
Field8 = c1*c2*c3*c4;
bulkUpdateOps.find({ "_id": doc._id }).updateOne({
"$set": { "Field8": Field8 }
});
if (counter % 500 == 0) {
bulkUpdateOps.execute();
bulkUpdateOps = db.collection1.initializeUnorderedBulkOp();
}
})
if (counter % 500 != 0) { bulkUpdateOps.execute(); }
但我仍然不知道从哪里开始实现上述,我应该创建一个js文件,它将为我做这个批量插入。从猫鼬连接到这个批量插入。
我试图创建一个包含此代码的js文件batch.js: //连接到DB VAR猫鼬=要求( '猫鼬'); var db = mongoose.connect('mongodb://127.0.0.1/mydb'); 以及上述解决方案中给出的代码; 但是,当我尝试编译它,我得到这些错误,请帮助! SyntaxError:意外的标记。 (module.js:359:25) at Module._compile(module.js:439:25) at Object.Module._extensions..js(module.js:474:10) at Module.load(module.js:356:32) at Function .Module._load(module.js:312:12) at node.js:906:3在Function.Module.runMain(module.js:497:10) 启动时(node.js:119:16) –