批量插入MongoDB和NodeJs

时间:2016-05-02 13:44:39

标签: node.js mongodb mongoose

我正在研究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文件,它将为我做这个批量插入。从mongoose连接到此批量插入。

1 个答案:

答案 0 :(得分:0)

为了在Mongoose中使用底层批量操作API,您应该能够通过mongoose模型中的.collection属性访问它。在使用API​​之前,请等待mongoose成功连接到db,因为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');
    });
});