猫鼬findoneandupdate返回更新的文档,但未在数据库中更新

时间:2018-07-03 17:38:13

标签: node.js database mongodb express mongoose

该模式是正确的,并且这些字段应该正在更新。因此,我从一个API调用中获得了部分数据。还有一些。我将在下面说明。 我的架构:

var coin = new Mongoose.Schema({
id: Number,
rank: Number,
source: String,
symbol: String,
name: String,
cap: String,
heat: Number,
circulating_supply: Number,
total_supply: Number,
max_supply: Number,
change: {
    hour: String,
    day: String
},
quotes: {
    USD: {
        price: Number,
        volume_24h: Number,
        market_cap: Number,
        percent_change_1h: Number,
        percent_change_7d: Number,
        percent_change_24h: Number
    },
    BTC: {
        price: Number,
        volume_24h: Number,
        market_cap: Number,
        percent_change_1h: Number,
        percent_change_7d: Number,
        percent_change_24h: Number
    },
    ETH: {
        price: Number,
        volume_24h: Number,
        market_cap: Number,
        percent_change_1h: Number,
        percent_change_7d: Number,
        percent_change_24h: Number
    }
},
high_low_24hr: {
    high: Number,
    high_time: Date,
    low: Number,
    low_time: Date
},
ath: {
    price_date: Date,
    price_usd: Number,
    price_btc: Number
},
month_high: {
    price_date: Date,
    price_usd: Number,
    price_btc: Number
},
month_low: {
    price_date: Date,
    price_usd: Number,
    price_btc: Number
},
last_updated: Number
});

第一个电话发出并获取此信息。这是当前在mongo中对象的外观:

{
"_id" : ObjectId("5b3548f7d78545f691195a9c"),
"quotes" : {
    "USD" : {
        "price" : 468.418,
        "volume_24h" : 1611470000,
        "market_cap" : 47058665940,
        "percent_change_1h" : -0.13,
        "percent_change_24h" : 0.07,
        "percent_change_7d" : 5.56
    }
},
"id" : 1027,
"name" : "Ethereum",
"symbol" : "ETH",
"rank" : 2,
"circulating_supply" : 100462975,
"total_supply" : 100462975,
"max_supply" : null,
"last_updated" : 1530638295
}

第二个电话发出并获得更多信息,主要是关于高点和低点。使用findOneAndUpdate的{new:true}参数,它返回更新的文档。返回的文档是正确的,但在数据库中未更改:

{ quotes:
{ USD:
  { price: 476.498,
    volume_24h: 1633220000,
    market_cap: 47862549879,
    percent_change_1h: 0.86,
    percent_change_24h: 4.83,
    percent_change_7d: 3.48 } },
ath:
{ price_usd: 1376.74,
 price_btc: 0.09671,
 price_date: 2018-01-14T05:00:00.000Z },
month_high:
{ price_usd: 475.78,
 price_btc: 0.07182,
 price_date: 2018-07-03T04:00:00.000Z },
month_low:
{ price_usd: 452.24,
 price_btc: 0.07106,
 price_date: 2018-07-02T04:00:00.000Z },
_id: 5b3548f7d78545f691195a9c,
id: 1027,
name: 'Ethereum',
symbol: 'ETH',
rank: 2,
circulating_supply: 100446486,
total_supply: 100446486,
max_supply: null,
last_updated: 1530569111 }

我也尝试过查找记录,并在单独的步骤中对其进行更新。我从findoneandupdate调用中没有收到任何错误。而且我认为我可以使用严格的参数来强制执行此操作,但我宁愿遵循这些模式。

根据请求,这是findOneAndUpdate调用。这个问题消失了,但是我想还是应该发布它。也许可以改善。

let update_ath_cc = (response, source, app) => {
  var data = response.data;
  var query = {symbol: data.symbol};

  db.coinModel.findOneAndUpdate(query, data, {new:true}, function(err, newRecord){
    if(err){
      console.log("database lookup error: " + err);
    }

    if(newRecord){
      console.log(data.symbol + " ath updated.");
      return true;
    } else {
      console.log(data.symbol + " not found for ath update.");
    }
  });
}

0 个答案:

没有答案