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();
But when I look at the user in the mongodb it still has the signup_date and first_name set...how do I effectively set this field as empty or null?
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();
Does it make a difference if you try the set method instead, like this:
user.set('first_name', null);
user.set('signup_date', null);
user.save();
Or maybe there's an error when saving, what happens if you do:
user.save(function (err) {
if (err) console.log(err);
});
Does it print anything to the log?
Just delete fields
delete user.first_name;
delete user.signup_date;
user.save();
Another option is to define a default value as undefined
for these properties.
Something like the following:
let userSchema = new mongoose.Schema({
first_name: {
type: String,
default: undefined
},
signup_date: {
type: Date,
default: undefined
}
})