How can I achieve this?
I’m new to Triggers, Is there any possibility of using Stitch in this kind of situation?
Kind of Cron job in MongoDB. @Lauren_Schaefer @007_jb
Ooooh, yes - this is a fun one. You can use a scheduled trigger to update the status. Atlas and Realm (formerly known as Stitch) both provide scheduled triggers.
The Realm docs include an example of how to configure a scheduled trigger.
Another option would be to dynamically determine status in your application based on the current date. You wouldn’t store the status in your document then – your app would just do a date comparison and act appropriately.
To add to @Lauren_Schaefer’s great advice, for on-premise deployment you can use Change Streams. And with regards the alternative option, exercise some caution embedding date related logic at application level because the device clock might be wrong.
One note on change streams. Database triggers (as opposed to scheduled triggers) are built on change streams. Change streams are awesome (here is a blog post I wrote about change streams in Node.js: Change Streams & Triggers with Node.js Tutorial | MongoDB), but I don’t know of a way to do anything scheduled with a change stream. Change streams watch for changes in your database and allow you to program actions based on those changes.
My 2 cents… you wouldn’t need a listening event stream when you’re working with scheduled events, you’ll just use a cron job/scheduled task to fire off a batch update based on the date condition.
Achieved the problem in this way:
I have abstracted and restricted the UI according to the date in the frontend.
But as mentioned by @007_jb, I feel there is disadvantage i.e job cannot be scheduled on random datetime which is added by the user in document, either I have to set interval in trigger (which is expensive) nor fire-up cron job from backend server.
By combining change stream and TTL indexes one can certainly achieve what you want.
You have a change stream (cs-1) on the main collection that watches for the random date/time field of interest.
When a change arrives on cs-1, you create an appropriate document in the TTL backed collection. This effectively schedule the job for future execution.
The second change stream (cs-2) on the TTL backed collection watches for deletes.
When a delete arrives on cs-2, you just run the job.