Mongoose findoneandupdate returns updated document but not updated in database

ⅰ亾dé卋堺 提交于 2021-01-27 04:40:01

问题


The schema is correct, and these fields should be updating. So, I get partial data from one API call. And some from another. Which I'll note below. My schema:

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

The first call goes out and gets this information. This is how the object looks at the moment in 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
}

The second call goes out and gets more information, mostly about highs and lows. Using findOneAndUpdate's {new:true} parameter, it returns the updated document. The returned document is correct, but untouched in the database:

{ 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 }

I have also tried finding the record, and updating it in separate steps. I get no errors from findoneandupdate call. And I think I can force it with a strict param, but I would rather adhere to the schemas.

By request, here is the findOneAndUpdate call. This issue went away, but I thought I'd post it anyway. Perhaps it can be improved.

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

来源:https://stackoverflow.com/questions/51160223/mongoose-findoneandupdate-returns-updated-document-but-not-updated-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!