Why Mongoose doesn't validate on update?

后端 未结 6 2547
故里飘歌
故里飘歌 2020-11-30 00:07

I have this code

var ClientSchema = new Schema({
  name: {type: String, required: true, trim: true}
});

var Client = mongoose.mode(\'Client\', ClientSchema)         


        
6条回答
  •  無奈伤痛
    2020-11-30 00:58

    mongodb does not run validation on update by default. in order to make validation works by default when update also, just before connecting to mongodb you can set global setting only ones like that:

    mongoose.set('runValidators', true); // here is your global setting
    
    mongoose.connect(config.database, { useNewUrlParser: true });
    mongoose.connection.once('open', () => {
        console.log('Connection has been made, start making fireworks...');
    }).on('error', function (error) {
        console.log('Connection error:', error);
    });
    

    and any built-in or custom validation will run on update also

提交回复
热议问题