简体   繁体   English

如何使用带有严格:false模式的猫鼬将字段添加到mongoDB文档中?

[英]How does one add a field to a mongoDB document using mongoose with a strict : false schema?

I'm trying to automate some data processing tasks for my IT firm. 我正在尝试为我的IT公司自动化一些数据处理任务。 I need to be able to cycle through a collection, find all documents with a given numerical ID (not ObjectID), and add them as an array field to documents from a different collection. 我需要能够遍历一个集合,找到具有给定数字ID(而不是ObjectID)的所有文档,并将它们作为数组字段添加到来自不同集合的文档中。 This is how I'm attempting it: 这是我尝试的方式:

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.");
    })

The update callback never fires, though I've tried a voter.find() with the same query and it returns exactly one voter every time I use a valid Registration_Number. 更新回调永远不会触发,尽管我尝试使用相同的查询尝试了一个表决器.find(),并且每次我使用有效的Registration_Number时,它都会恰好返回一个表决器。 I need to automate this with unpredictable field names, so I'd like to avoid using schemas if at all possible. 我需要使用不可预测的字段名称来自动执行此操作,因此,我尽可能避免使用架构。 My current schema is bare bones: 我当前的架构是白手起家:

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

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

Anyone have an idea how I can make this work? 有人知道我该如何进行这项工作吗? This question is similar to quite a few others, but every one I've seen is answered with model.update(), which I can not seem to get working. 这个问题与很多其他问题相似,但是我见过的每个问题都通过model.update()来回答,我似乎无法正常工作。

My code runs for a while printing out only ElectionDate, and the update callback never fires. 我的代码只运行一会儿,仅打印ElectionDate,而更新回调从未触发。 When I let it run it ends on this error: 当我让它运行时,它在此错误上结束:

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

The solution, though not pretty, was to modify the doc._doc and use the overwrite: true option in the update function. 解决方案虽然不是很漂亮,但是却修改了doc._doc并使用了update函数中的overwrite:true选项。 Full code: 完整代码:

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.");
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM