Mongoose not saving nested object

后端 未结 1 1789
终归单人心
终归单人心 2021-02-08 19:46

I\'m puzzled as of why Mongoose isn\'t saving my object:

var objectToSave = new ModelToSave({
  _id : req.params.id, 
  Item : customObject.Item //doesn\'t save          


        
1条回答
  •  情深已故
    2021-02-08 20:27

    I came across this frustrating situation and was a little surprised by the documented solution from Mongoose's website.

    so what this means is to save nested array/object properties (Item in your case), you need to be explicit in specifying the change .markModified('Item')

    var objectToSave = new ModelToSave({
      _id : req.params.id, 
      Item : customObject
    });
    objectToSave.markModified('Item');
    objectToSave.save();
    

    Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. 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.

    -- http://mongoosejs.com/docs/schematypes.html#mixed

    0 讨论(0)
提交回复
热议问题