For some reason, pre nor post event is getting triggered when data is updated thru updateMany function in Mongoose 5.2.7.
Init event is getting triggered though.
Following is the code
const schema = new mongoose.Schema({
name : { type: String },
address : { type: String },
}, { versionKey: false });
schema.set('collection', 'test');
schema.pre('save', function() {
console.log('--- PRE SAVE ----');
})
schema.post('save', function() {
console.log('--- POST SAVE ----');
})
What could I be doing wrong?
Here
pre('save', () => {})
is getting triggered withsave
call only andupdateMany
andsave
are not the same. You need to callsave()
to triggerpre and post save
events here.