簡體   English   中英

如何使用帶有嚴格:false模式的貓鼬將字段添加到mongoDB文檔中?

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

我正在嘗試為我的IT公司自動化一些數據處理任務。 我需要能夠遍歷一個集合,找到具有給定數字ID(而不是ObjectID)的所有文檔,並將它們作為數組字段添加到來自不同集合的文檔中。 這是我嘗試的方式:

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

更新回調永遠不會觸發,盡管我嘗試使用相同的查詢嘗試了一個表決器.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...

解決方案雖然不是很漂亮,但是卻修改了doc._doc並使用了update函數中的overwrite:true選項。 完整代碼:

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