Discussion

Forced update for Mixed type in Mongoose

There is a Mixed data type in Mongoose schema definition.

The problem with Mixed type is, it doesn’t recognize update event for Mixed type.

// /models/coin.model.js
const coinSchmea = mongoose.Schema({
  ...
  history: [{
    type: mongoose.Schema.Types.Mixed
  }
  ...
});
// in the controller
const coin = await Coin.findOne({symbol: 'BTC'});
coin.history = newValue; 
await coin.save() // this doesn't actually update the value

To fix this, you should call markModified function.

coin.history = newValue;
coin.markModified('history'); // enforce update
await coin.save();

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *