如何将数据推送到集合中的猫鼬数组?

时间:2019-05-15 20:33:44

标签: javascript node.js mongoose

我正在创建一个CronJob,以每隔00:01更新我的key集合中的每个currencies。这是我的currencies选择模式:

const CurrencySchema = new mongoose.Schema({
    currency: {
        type: String,
        unique: true,
        required: true
    },
    name: {
        type: String,
        required: true
    },
    exchange_rate: {
        type: SchemaTypes.Double,
        required: true
    },
    spread: {
        type: SchemaTypes.Double,
        required: true,
        default: 0,
        select: false
    },
    lastUpdate: {
        type: Date
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    history: [{
        date: Date,
        rate: SchemaTypes.Double
    }]
});

我试图创建一个像这样的CronJob:

const CronJob = require("cron").CronJob;
const currencies = require("../../models/currencies");

module.exports = new CronJob("* 1 0 * * *", async function() {
    await currencies.find({}).map(currency => currency.history.push({
        date: Date.now(),
        rate: currency.exchange_rate
        })
    )
});

因此,对于每个currency,我想将pushhistory的货币分别Date.now()转换为exchange_rate数组。 / p>

问题是我得到了这个异常:

  

UnhandledPromiseRejectionWarning:TypeError:无法读取属性   未定义的“推送”

我应该如何解决?

谢谢!

编辑:map函数中的console.log(currency)显示如下:

[
  {
    _id: 5cbdefd4f5bcec257fcec791,
    currency: 'VES',
    exchange_rate: 5321.709437805213,
    createdAt: 2019-04-22T16:46:12.350Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:40:08.649Z,
    name: 'Venezuelan Bolivar',
    history: []
  },
  {
    _id: 5cbdf078f5bcec257fcec792,
    currency: 'BTC',
    exchange_rate: 7290,
    createdAt: 2019-04-22T16:48:56.182Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:01.122Z,
    history: []
  },
  {
    _id: 5cbe1ebccd6e6a2b4738070d,
    currency: 'BRL',
    exchange_rate: 4.2382007085048015,
    createdAt: 2019-04-22T20:06:20.796Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:02.817Z,
    name: 'Brazilian Real',
    history: []
  },
  {
    _id: 5cbe1ec8cd6e6a2b4738070e,
    currency: 'INR',
    exchange_rate: 78.43526089163238,
    createdAt: 2019-04-22T20:06:32.322Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:02.814Z,
    name: 'Indian Rupee',
    history: []
  },
  {
    _id: 5cbe1ecfcd6e6a2b4738070f,
    currency: 'ZAR',
    exchange_rate: 15.984316920438957,
    createdAt: 2019-04-22T20:06:39.513Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:03.135Z,
    name: 'South African Rand',
    history: []
  },
  {
    _id: 5cbe1ed9cd6e6a2b47380710,
    currency: 'ARS',
    exchange_rate: 50.264175514403284,
    createdAt: 2019-04-22T20:06:49.520Z,
    __v: 0,
    lastUpdate: 2019-05-15T20:41:04.134Z,
    name: 'Argentinian Peso',
    history: []
  }
]

所以我假设正在创建另一个数组,而不是返回每个单独的对象

1 个答案:

答案 0 :(得分:1)

根据Mongoose Update API,正确的语法应为:

await currencies.find({}).map(currency => currencies.update({ "_id": currency._id},
  { "$push": { "history": { date: Date.now(), rate: currency.exchange_rate } } },
  function (err, raw) {
    if (err) return handleError(err);
    console.log('The raw response from Mongo was ', raw);
  }
));

根据新信息进行更新。可能需要显式返回Mongoose调用,并且可以肯定地进行重构:

await currencies.find({}).map(currencyList => { 
  currencyList.forEach(currency =>  {
    currencies.updateMany({ "_id": currency._id},
      { "$push": { "history": { date: Date.now(), rate: currency.exchange_rate } } },
      function (err, raw) {
        if (err) return handleError(err);
        console.log('The raw response from Mongo was ', raw);
      }
    )
  })
});