[Mongoose] Schema.Types.Mixed Requires .markModified Before save

Tadashi Shigeoka ·  Thu, February 28, 2013

One of the reasons why you can’t save instances with save in Node.js + Mongoose is Schema.Types.Mixed.

It seems that Mongoose doesn’t automatically detect that a value has changed when the property type is Schema.Types.Mixed.

mongoose | Mongoose

To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.

[Source]:Mongoose SchemaTypes v3.5.6

The usage of the markModified method is as follows:

var schema = new Schema({
  mixed:   Schema.Types.Mixed
})

// example use

var Thing = mongoose.model('Thing', schema);

var m = new Thing;
m.mixed = {[ any: { thing: 'i want' } ]};
m.markModified('mixed');
m.save(callback);

That’s all from the Gemba where we want to master Mongoose’s Schema.Types.Mixed.