set field as empty for mongo object using mongoose


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

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

But when I look at the user in the mongodb it still has the signup_date and first_name do I effectively set this field as empty or null?

9/28/2012 6:45:16 PM

Accepted Answer

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;;
9/28/2012 12:31:59 PM

Does it make a difference if you try the set method instead, like this:

user.set('first_name', null);
user.set('signup_date', null);;

Or maybe there's an error when saving, what happens if you do: (err) {
    if (err) console.log(err);

Does it print anything to the log?

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow