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 ----');
})'save', function() {
  console.log('--- POST SAVE ----');

What could I be doing wrong?

2 Answers

Shivam Pandey On

Here pre('save', () => {}) is getting triggered with save call only and updateMany and save are not the same. You need to call save() to trigger pre and post save events here.

Me Unagi On

Resolved by adding schema.pre('updateMany') method.