set field as empty for mongo object using mongoose

后端 未结 5 783

I\'m calling user.save() on an object, where I set user.signup_date = null;

user.first_name = null;
user.signup_date = null;

user.save();

5条回答
  •  清歌不尽
    2020-12-01 11:16

    To remove those properties from your existing doc, set them to undefined instead of null before saving the doc:

    user.first_name = undefined;
    user.signup_date = undefined;
    
    user.save();
    

    Confirmed as still working in Mongoose 5.9.7. Note that the field you're trying to remove must still be defined in your schema for this to work.

提交回复
热议问题