【发布时间】:2016-05-03 18:05:41
【问题描述】:
我正在开发 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 得到了一个结构化的答案,就像这样:
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: //connect to DB var mongoose = require('mongoose'); var db = mongoose.connect('mongodb://127.0.0.1/mydb');以及上述解决方案中给出的代码;但是当我尝试编译它时,我得到了这些错误,请帮忙! SyntaxError: Unexpected token 。在 Module._compile (module.js:439:25) 在 Object.Module._extensions..js (module.js:474:10) 在 Module.load (module.js:356:32) 在 Function.Module._load (module.js:312:12) 在 Function.Module.runMain (module.js:497:10) 在启动时 (node.js:119:16) 在 node.js:906:3