繁体   English   中英

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

[英]How to push data to a mongoose array in collection?

我正在创建一个CronJob,以每隔00:01更新currencies集合中的每个key 这是我的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 ,我想push当时的Date.now()和货币的exchange_rate push送到history数组。

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

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

我应该如何解决?

谢谢!

编辑: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: []
  }
]

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

根据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);
      }
    )
  })
});

暂无
暂无

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

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