Mongoose - Listen for events on bulk operations

Viewed 737

I am using bulk operations to upsert documents in my collection. After each such upsert I want to trigger a certain action.

I know that there are the pre/post hooks for mongoose models, however using "save" or "update" on those does not get triggered by the bulk operations.

Does anybody know a solution how to listen for bulk operations?

This is an example for my bulk operation:

const bulk = MyModel.collection.initializeUnorderedBulkOp();
bulk.find({someField: "someValue"}).upsert().updateOne({someField: "someNewValue"});

And my hooks:

MyModel.post("save", (doc: any) => {
    console.log("In save hook");
});
MyModel.post("update", (doc: any) => {
    console.log("In update hook");
});
0 Answers
Related