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
Santiago Trujillo
Tykaty, you can try to use 2 features of mongodb:
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.
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.