Triggers are getting executed for the documents which are inserted when the trigger was disabled

Hi,

I just noticed some weird thing about triggers. I followed the below steps,

  1. I created a new collection test1.
  2. I created a database trigger for test1 with insert, update and replace Operation types.
  3. I disabled the above trigger, because I wanted to transform and merge the data to test1 from temp collection using aggregates. During this process, I did not want trigger to fire, so I disabled the triggers on test1 collection.
  4. After merging the data to test1, I enabled the triggers on test1 collection again but surprisingly trigger has fired for all the documents which were inserted to test1 when trigger was disabled (i.e. during aggregate migration). I am not sure how did it happen. And, I want to stop this from happening because merging the data from temp to test1 will have millions of records and I don’t want trigger to get fired for all those documents during this process.

Can anyone please let me know what is wrong with my approach? Basically, I don’t want trigger to get fired for the documents which are inserted/updated/replaced when trigger was disabled.

Thanks,
Vinay

Hi Vinay,

Yes this is expected as when the index is enabled after its disabled, the trigger automatically uses the last stored resume token to try and cover all operations since last activity.

This behaviour is currently the default and unchangeable behaviour for enable/disable.

The only thing you can do is to export or save the trigger function seperately and delete the trigger. Recreate it pointing to the original function.

Consider filing a call in https://feedback.mongodb.com to allow this to work as with suspended trigger, where you can checkbox if you wish to use a resume token or start fresh.

CC: @Drew_DiPalma I saw this confusion with a few users.

Best
Pavel

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.