Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set field as empty for mongo object using mongoose

Tags:

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?

like image 514
chovy Avatar asked Sep 28 '12 09:09

chovy


1 Answers

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.

like image 56
JohnnyHK Avatar answered Oct 20 '22 04:10

JohnnyHK