mongoose: Schema.type.Mixed and deep modification does not update document

When I try to update a deep nested field, the query is well sent to MongoDB but when I do a find on this doc, the field is not updated.

Example

var DtSchema = mongoose.Schema({
     nested : Schema.type.mixed
});

var Dt = mongoose.model('Dt', DtSchema)

var test = new Dt({nested : { a : 'test', b : { c : { d : null } } } });

test.save(function(err, _test) {
    _test.nested.b.c.d = 'Hello !';
    _test.save(function(err, _test) {
       // _test is okay, the field nested.b.c.d is updated 

       Dt.findOne({_id : _test._id}, function(err, doc) {
           // Here The doc has not the nested.b.c.d field updated
       });
    })
});

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 16

Most upvoted comments

Did you guys try using markModified()? Mixed types won’t save updates unless you tell Mongoose something has changed

If you do this, it will work.

var nested = JSON.parse(JSON.stringify(_test.nested)); //now changed the nested object. nested.a = “new value” _test.nested = nested; _test.save();