Database Triggers based on document date

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)

Hi @Rishi_uttam,

You can use scheduled triggers based on cron tab expressions to pull the data using Realm triggers, no need for lambda.

Having said that, if you need to perform actions based on expiry of documents you can see the following workaround I offered on that thread:

Please let me know if you have any additional questions.

Best regards,
Pavel

Hi Pavel,

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

what are my options?

Hi @Rishi_uttam,

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:

  1. Create a collection that will contain the schedule data. In my example: schedule collection.

  2. 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

    E.g

    const sc_collection = context.services.get("LiveMig").db("applicationData").collection("schedule");
    const doc = sc_collection.insertOne({_id:changeEvent.fullDocument._id,triggerDate: changeEvent.fullDocument.enddate  });
    
  3. Create a TTL index on the triggerDate in the schedule collection as following:

    createIndex( { "triggerDate": 1 }, { expireAfterSeconds: 0 } )
    
  4. 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

    const user_collection = context.services.get("LiveMig").db("applicationData").collection("user_coll");
    const doc = await user_collection.findOne({ _id: changeEvent.documentKey._id});
    

Working with Realm API to create triggers ad-hoc

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.

  1. Create a database trigger which will be activated on saving the record in the original collection.
  2. 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:

  1. If not reusing same trigger for scheduling - management and pruning of the created triggers is required.
  2. config.schedule accepts Cron Expressions which may result in some limitations on setting up the scheduled triggers.

Best regards,
Pavel

1 Like

Hi Pavel.

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

Hi @Rishi_uttam ,

You can use a triggering collection that will delete a dummy trigger document and not the real document you want to operate on.

I have a similar example in this article:

Thanks
Pavel

1 Like

Wonderful, I’ll try and report soon.

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