Update the object in a collection on dataTime automatically

I need to update the document’s status field automatically in the document without firing an API call.

For example: the document below
If my current date is <= July 11th, then status = "available"

  _id: "adsasdad2347091024",
  ticket: "musical concert",
  status: "available",
  endDateTime: "2021-07-11" 

If my current date is July 12 then it should automatically update the status to “unavailable” without making an API call from the server.

If my current date is > July 11th, then status = "unavailable"

  _id: "adsasdad2347091024",
  ticket: "musical concert",
  status: "unavailable",
  endDateTime: "2021-07-11"

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.

An example would be helpful.
Thank you.

1 Like

Hi Rakshith,

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.

Atlas: https://docs.atlas.mongodb.com/triggers/#scheduled-triggers

Realm: https://docs.mongodb.com/realm/triggers/#scheduled-trigger

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.

1 Like

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.

By the way @Rakshith_79674, I’m not an active contributor.


Great point about the device clock!

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: https://www.mongodb.com/developer/quickstart/nodejs-change-streams-triggers), 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.

1 Like

Great detailed blog post there @Lauren_Schaefer :bowing_man:

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.

If there is any better option, please let me know @007_jb @Lauren_Schaefer

By combining change stream and TTL indexes one can certainly achieve what you want.

  1. You have a change stream (cs-1) on the main collection that watches for the random date/time field of interest.
  2. 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.
  3. The second change stream (cs-2) on the TTL backed collection watches for deletes.
  4. When a delete arrives on cs-2, you just run the job.

Currently, I have a rest API server, do I have to create the Socket for this use case?

@steevej @007_jb @Lauren_Schaefer
Is this the process, guide me if I’m wrong

Step-1 => Creating the TTL index when the document is inserted, using database trigger.

  1. By creating database trigger on insert operation on collection
  2. Selecting startData value form changeEvent.fullDocument.startDate
  3. CreateIndex on the startDate with expireAfterSeconds (by calculating seconds from current dateTime from system & startDate)

I believe that as soon as TTL index expires it’ll run database trigger. (correct me If I’m wrong)

Step-2 => Once the TTL index expires triggering an insert operation by updating the changeEvent.fullDocument.productStatus

  1. Insert the document
  2. Remove TTL Index

I assume that this can be done through a database trigger.