批量插入MongoDB和NodeJ [英] Batch insert MongoDB and NodeJs

查看:83
本文介绍了批量插入MongoDB和NodeJ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB 2.6.9和NodeJs 0.10.37,并根据我之前的问题 chridam 那里得到了一个结构化的答案,就像这样:

I'm working on MongoDB 2.6.9 and NodeJs 0.10.37 and according to my previous question MongoDB calculating score from an existing fields and put it in a new field in the same collection and I have got a structured answer for doing that from chridam just like this :

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文件,它将为我完成此批处理插入.从猫鼬连接到此批处理插入.

But I still don't know from where start to achieve the above, I should create a js file which will do this Batch insert for me. From mongoose connection to this Batch insert.

推荐答案

为了在Mongoose中使用基础的批量操作API,您应该能够通过Mongoose模型中的.collection属性对其进行访问.在使用API​​之前,请等待猫鼬成功连接到数据库,因为猫鼬当前不真正支持"initializeOrderedBulkOp()",因为它无法与其内部缓冲系统一起使用.因此,类似以下实现(未经测试)的东西应该可以为您提供一个想法:

In order to use the underlying bulk operations API in Mongoose, you should be able to access it via the .collection property from the mongoose model. Before using the API, wait for mongoose to successfully connect to the db since Mongoose doesn't really support the "initializeOrderedBulkOp()" currently as it doesn't work with its internal buffering system. So something like the following implementation (not tested) should give you an idea:

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');
    });
});

这篇关于批量插入MongoDB和NodeJ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆