• Home
  • Jobs
  • Courses
  • Teachers
  • For business
  • Blog
  • ES/EN

0

17
Views
What is the best way to notify users that document expired in database?

In my application users create documents which are then saved in the database. The document has expireAt field which is set to 30 days ahead from the date it is created. After the expiration date the document is considered as inactive.

So, what I want is to send an email to the user after expiration date to notify him that his document is now inactive. The only solution I see is to create a cron job and periodically poll the database for expired documents.

But I'm not sure if periodical polling is a good approach and would like to know if there are other ways of doing this.

P.S. The app is built with nodejs + mongoDB

13 days ago ·

Santiago Trujillo

2 answers
Answer question

0

Tykaty, you can try to use 2 features of mongodb:

  • Expire data https://docs.mongodb.com/manual/tutorial/expire-data/
  • Change events https://docs.mongodb.com/manual/reference/change-events/ https://docs.mongodb.com/manual/changeStreams/#watch-a-collection--database--or-deployment

The idea is simple. You store the documents as before. Add a collection that you will watch and add there objects which should expire, link each document in your original collection to a corresponding object to be expired(1 to 1 link). When the object is deleted by mongo engine(expired), you get a notification with it's _id. Find this _id in your original collection of documents to understand which doc was expired. Here you are.

Of course, you can start with polling, final solution depends on the data and it's usage, the load as well.

13 days ago · Santiago Trujillo Report

0

If your use case is just removing the expired doc, you could use TTL feature of mongodb.

Since you need to send an email, the best option is cron job as you already thought of. Yes, periodically polling is a good option and works in most of the use cases and you do have control over it.

As per your requirement, you could poll once per day. If you are still need to care till minute level of expiry, you could do that by subtracting 24 hours from your actual query and send an alert for user convenience.

13 days ago · Santiago Trujillo Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.