I have a Mongoose (using current version) schema with a subdocument:
var mySchema = new Schema({
subdocument: { property1: { type: String } }
});
var myModel = mongoose.model('My-Model', mySchema);
Now I'm trying to update an existing document and to remove the subdocument like so using doc.subdocument = {}:
new myModel({ name: 'test', subdocument: { property1: 'test' }}).save(function(err, doc) {
console.log('saved doc:\n', doc);
doc.subdocument = {}; // remove content of subdocument
doc.save(function(err, doc) {
console.log('updated doc:\n', doc); // subdocument: null
myModel.findById(doc._id, function(err, doc) {
console.log('retrieved doc:\n', doc); // subdocument: { property1: 'test' }
mongoose.disconnect();
});
});
});
The callback in doc.save returns the document with subdocument: null, so I assumed, the update worked as expected. However, when examining the database, the subdocument's content is still there -- this can be seen by above's sample code when I retrieve the document again.
The document in the DB looks as:
{ subdocument: { property1: 'test' }, __v: 0, _id: 57593a8130f2f7b6a12886b1 }
Is this a bug or a fundamental misunderstanding?