How to set trigger on the basis of date present in specific document or from many document the trigger should excecute every time

I want to make trigger such a that when user create contest from app & document will be inserted with expiry time stamp of next 24hours in mongodb. So when that expiry time stamp time arrives it will automatically set value to fast so my contest will end on App.

Hi @Its_Me,

Maybe you could leverage Time To Live (TTL) indexes so your document would be deleted within 60 seconds of its expiry date and be deleted. You can use this event to start a trigger in Realm as well.

Cheers,
Maxime.

2 Likes

Thanks for Answer. I learnt something new by this answer but my problem statement is to change field value like true to false so contest will be disabled. Not deleted

2 Likes

IF your update doesn’t need to be precise down to the second, but can happen somewhere between 0 and 60 seconds after the expiration date, you could use TTL indexes:

  • When you insert a document in your stuff collection with an expiration_date and a field active: true
  • Create a Realm Trigger that inserts a new document in a new collection “events” like this:
{
  "_id": <same as the other doc in the stuff collection>,
  "expiration_date": <date when you want to udpate the field>
}
  • Create a TTL index on the collection events
db.events.createIndex( { "expiration_date": 1 }, { expireAfterSeconds: 0} )
  • With this, the document in the events collection will be deleted when the date is reached.
  • Create another Realm Trigger that catches this deletion. Grad the _id of the deleted item from the changeEvent.
  • In the function, update the document in the stuff collection with the same _id => active: false.

We have a blog post coming up soon on https://www.mongodb.com/, it’s currently in review :smiley:! Ping @Pavel_Duchovny.

IF you need a real time update of that active field, you need to implement another solution based on a realm time software.

I hope this helps. I tried to stay as concise as possible, but please let me know if you need more details of course. Hopefully the blog post is coming soon :grinning_face_with_smiling_eyes:

Basically this is a derivation of a notification system.

Cheers,
Maxime.

EDIT PS: The same concept is explained in this blog post:

https://www.mongodb.com/article/triggers-tricks-data-driven-schedule/

4 Likes

Thanks @MaBeuLux88 such a great support encourages me to keep learning. I will try it. Hopefully i am sure it will work else i will reach you again.

3 Likes

Thanks a lot for your comment. I’m really happy my post helped you. See you in the next topic :smiley: !

3 Likes

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