Here and here is solution for unsetting some fields which works fine unless they are nested. When I tried the following thing 'null' is being saved against the field instead of unsetting it. How can I get it working ?
PostSchema = new Schema({
title : String
, slug : String
, publish : {
done : {type:Boolean, default:false}
, on : Date
, by : ObjectId
}
, created : Date
, ...
});
PostSchema.pre('save', function(next) {
if(!this.isNew && this.isModified('publish') && !this.publish.done) {
//console.log('OK I am going to unset publish.on, publish.by ');
this.publish.on = undefined;
this.publish.by = undefined;
}
// do some other stuffs
next();
});
EDIT
I got following log :
Mongoose: posts.update({ _id: ObjectId("53e3695289469b7136000033") }) { '$set': { lastModifiedOn: new Date("Fri, 08 Aug 2014 06:47:06 GMT"), publish: { done: false, on: undefined, by: undefined } } } {}