3

I have a requirement that when a date attribute field is passed, that we would like to trigger two things:

  1. to move the record to be deleted to another table.
  2. to call a function to do other actions.

I understand TTL is only to delete a record when the date field is tripped. Can I hook extra logic to it?

Thanks!

Bluetoba
  • 885
  • 1
  • 9
  • 16

2 Answers2

2

Depending on the requirements there could be quite a few ways to do this.

One way is to execute a script periodically, and run a query to filter documents that have passed certain date value. For each of the documents, perform a document migration to another table and extra actions.

Alternatively is to use MongoDB Change Streams. The trick however, is that delete events from change stream do not return the document itself (because it's already been deleted).

Instead if you were to update a field for documents that have passed certain date value you could listen for the update events. For example, sets a field value to expired:true.

Worth mentioning that if you're going down the route of change streams update events, you could utilise MongoDB Stitch Triggers (relying on change streams). MongoDB Stitch database triggers allow you to automatically execute Stitch functions in response to changes in your MongoDB database.

Wan B.
  • 18,367
  • 4
  • 54
  • 71
1

I suggest write a function and call it via scheduler. That will be the better option to do it.

Nilay shah
  • 77
  • 3
  • What about using the oplog after TTL deletes the record? Can we use logic to check the oplog entry to insert records in another table? Is reading oplog reliable? – Bluetoba Jul 26 '18 at 22:03