2016-05-02 135 views
1

我工作的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文件,它將爲我做這個批量插入。從貓鼬連接到這個批量插入。

+0

我試圖創建一個包含此代碼的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) –

回答

0

爲了在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'); 
    }); 
}); 
+1

它工作乾淨(y)非常感謝你 –