2015-10-15 129 views
1

我正在尝试为我的IT公司自动执行一些数据处理任务。我需要能够遍历集合,找到具有给定数字ID(不是ObjectID)的所有文档,并将它们作为数组字段添加到来自不同集合的文档。这就是我如何尝试它:如何使用mongoose和strict:false模式将字段添加到mongoDB文档中?

voter.find({"District" : {"$eq" : "D"}}).stream() 
    .on('data', function (doc) { 

     var reg = new RegExp(doc._doc.Registration_Number, "i"); 
     history.find({"Registration_Number" : {"$eq" : Number(doc._doc.Registration_Number)}}, function (err, docs) { 
     var arr = new Array(); //In the context of a single voter! 
     u.each(docs, function (v, k) { 
      if (v) { 
      console.log(v._doc.ElectionDate); //Only if Registration_Number of voter matches history doc. 
      arr.push(v._doc.ElectionDate); 
      } 
     }); 

     //Now we have array of history 
     doc.update({"Registration_Number" : {"$eq" : Number(doc._doc.Registration_Number)}}, {VotingHistory : arr}, {multi : true}, function (error, num) { 
      if (err) console.log("SOMETHING WENT VERY WRONG"); 
      console.log(dat); 
     }); 

     }); 
    }) 
    .on('error', function (err) { 
     console.log(err); 
    }) 
    .on('end', function() { 
     console.log("Ending voter stream."); 
    }) 

更新回调不闪光,但我已经尝试了voter.find()具有相同的查询和它的每一个我使用一个有效的Registration_Number时间正好返回一个选民。我需要使用不可预知的字段名称自动执行此操作,所以我想尽可能避免使用模式。我目前的模式是裸露的骨骼:

var mongoose = require('mongoose'); 
var voter = new mongoose.Schema({}, { 
    strict: false, 
    collection: 'voters' 
}); 

module.exports = mongoose.model('voter', voter); 

任何人都有一个想法,我可以使这项工作?这个问题类似于其他一些问题,但是我看到的每个问题都以model.update()回答,我似乎无法正常工作。

我的代码运行一会儿只打印出ElectionDate,并且更新回调从不触发。当我让它运行在此结束错误:

MY_DIRECTORY\THE_APP\node_modules\mongoose\lib\query.js:2008 
     oldCb(error, result ? result.result : { ok: 0, n: 0, nModified: 0 }); 
    ^
TypeError: object is not a function 
    at callback (MY_DIRECTORY\THE_APP\node_modules\mongoose\lib\query.js:2008:7) 
    at Object.<anonymous> (MY_DIRECTORY\THE_APP\node_modules\mongoose\node_modules\kareem\index.js:160:11) 
    at Object._onImmediate (MY_DIRECTORY\THE_APP\node_modules\mongoose\node_modules\mquery\lib\utils.js:137:16) 
    at processImmediate [as _immediateCallback] (timers.js:354:15) 
15 Oct 09:13:31 - [nodemon] app crashed - waiting for file changes before starting... 

回答

0

的解决方案,虽然不漂亮,是修改doc._doc和使用覆盖:在更新功能,真正的选项。完整代码:

voter.find({"District" : {"$eq" : "D"}}).stream() 
    .on('data', function (doc) { 

     var reg = new RegExp(doc._doc.Registration_Number, "i"); 
     history.find({"Registration_Number" : {"$eq" : Number(doc._doc.Registration_Number)}}, function (err, docs) { 
     var arr = new Array(); //In the context of a single voter! 
     u.each(docs, function (v, k) { 
      if (v) { 
      console.log(v._doc.ElectionDate); //Only if Registration_Number of voter matches history doc. 
      arr.push(v._doc.ElectionDate); 
      } 
     }); 

     //Now we have array of history 
     doc._doc.VotingHistory = arr; 
     console.log(doc._doc); 
     voter.update({ "Registration_Number" : doc._doc.Registration_Number}, doc._doc, {overwrite : true }, function (er, num) { 
      if (er) console.log("ERRRRRRRRRRRRRRRRRRRRRRRRRR"); 
      console.log("NUMBER AFFECTED (should always be 1) : " + num); 
     }); 

     }); 
    }) 
    .on('error', function (err) { 
     console.log(err); 
    }) 
    .on('end', function() { 
     console.log("Ending voter stream."); 
    }); 
相关问题