【问题标题】:Batch insert MongoDB and NodeJs批量插入 MongoDB 和 NodeJs
【发布时间】: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

标签: node.js mongodb mongoose


【解决方案1】:

为了在 Mongoose 中使用底层批量操作 API,您应该能够通过 mongoose 模型中的 .collection 属性访问它。在使用 API 之前,请等待 mongoose 成功连接到数据库,因为 Mongoose 目前并不真正支持“initializeOrderedBulkOp()”,因为它不能使用其内部缓冲系统。所以类似下面的实现(未测试)应该给你一个想法:

var mongoose = require('mongoose'),
    express = require('express'),
    Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/mydb');
var collection1Schema = new Schema({},{ strict: false, collection: 'Collection1' }),    
    MyModel = mongoose.model("MyModel", collection1Schema );

mongoose.set('debug', true);

mongoose.connection.on("open", function (err) {
    if (err) throw err;  
    var bulkUpdateOps = MyModel.collection.initializeUnorderedBulkOp(), 
        counter = 0;

    MyModel.find({}).lean().exec(function (err, docs) {
        if (err) throw err; 

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

            counter++;

            bulkUpdateOps.find({ "_id": doc._id }).updateOne({
                "$set": { "Field8": Field8 }
            });

            if (counter % 500 == 0 ) {
                bulkUpdateOps.execute(function(err, result) {
                    if (err) throw err;  
                    bulkUpdateOps = MyModel.collection.initializeUnorderedBulkOp();
                    console.log(result);
                });
            } 

        });     

        if (counter % 500 != 0 ) {            
            bulkUpdateOps.execute(function(err, result) {
                if (err) throw err;  
                console.log(result);
            });         
        }       
    });

    var app = express();
    app.listen(3000, function () {
        console.log('now listening on http://localhost:3000');
    });
});

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2017-10-05
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多