Mongoose : save nested object on instance

Posted on: 2017-11-26

Say you have the following schema in mongoose :

const schema = new mongoose.Schema({ name: String, price : Number, features : Object });

If you have created an instance from model and you try to update it, be aware of this :

const product = await Product.findOne({ name : "Fidget Spinner" }); product.features.coolColor = true; await; // changes on features object wont be saved ! // you have to tell mongoose to save the changes : product.markModified('features'); // and then save : await; // changes on features object are saved :-)

This is because mongoose can't detect changes on an mixed type attribute.