set field as empty for mongo object using mongoose

后端 未结 5 784

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:32

    On Mongoose documentation (Schema Types), you can go to the Arrays explanation. There, it says this:

    Arrays are special because they implicitly have a default value of [] (empty array).

    var ToyBox = mongoose.model('ToyBox', ToyBoxSchema);
    console.log((new ToyBox()).toys); // []
    

    To overwrite this default, you need to set the default value to undefined

    (I've made an addition inside the toys element)

    var ToyBoxSchema = new Schema({
         toys: {
              type: [{
                  name: String,
                  features: [String]
              }],
              default: undefined
         }
    });
    

提交回复
热议问题