Mongodb Atlas support database triggers, from what I understand that is that only on insert, update and delete operation? My questions is can I send either a webhook or a stitch function to be triggered based on a document date in a collection?
I.e. if ccExpiryDate = Date.now() trigger a webhook to my api or trigger a stitch function.
I feel like this is possible with Atlas no?
My other options is to use a lambda function to poll the database daily for expired dates, (seems inefficient)
Cron expressions cant reach in to a document collection to check dates can they?
would you mind providing an example whereby a webhook or function is run based on a document date field.
When you say “Use schedule triggers based on cron tab expressions” That means run the function daily and check if any documents match" This could mean the function could run unnecessarily but didn’t have to since no documents returned that match the criteria?
The solution you provided to @ilker_cam was a good one, however that would require me creating a TTL index which will delete the document (i dont want to delete the document) i want to check dates within it and run webhook or function.
a) i could run a lambda function that checks the document date every minute, but that makes no sense
b) it would be great if mongo could trigger the function on its own without being called
I can offer 2 options which I once worked with a colleague on (Big kodos to @Irena_Zaidman!!!) .
Working with a temporary schedule collection
This option is generally similar to the TTL Idea with some tweaks.
The implementation will be as following:
Create a collection that will contain the schedule data. In my example: schedule collection.
Create a database trigger that is activated on the INSERT of the record in the original collection. The trigger will populate the schedule collection with same_id field from and the original collection
Create another database trigger that is activated on the DELETE of the record in the schedule collection.
Use the _id of the deleted record (which is identical to the record in the original collection) to retrieve the required record of the original collection and run the required scripts.
e.g
The implementation will involve creating scheduled triggers, using Realm API. The triggers will be created when the record containing the enddate is saved to the collection.
The trigger will run a function using the Realm administration API to create a scheduled trigger by specifying the config.schedule parameter as the value of triggerDate field.
(Optional- reusing same trigger, by updating the config.schedule parameter - a possible complication is overriding other schedules )
Limitations to this solution:
If not reusing same trigger for scheduling - management and pruning of the created triggers is required.
config.schedule accepts Cron Expressions which may result in some limitations on setting up the scheduled triggers.
A lot may have changed since this post was created… I am still having an issue of easily triggering a function based on a document value (i.e date value) Cron expressions are ment to be run multipole times, however I only want to run a function once (like send an email based on a date/time)
Currently i poll the database daily and check the dates for all records, and launch the function (but seems very inefficient)
Note that I do not want to delete the document, I only want to check if the date field is passed. Can i still use a TTl trigger for this?)
Is the above solution still the best option ? Thanks