简体   繁体   English

让到期日期触发MongoDB中的函数?

[英]Let an expiry date trigger a function in MongoDB?

I am trying to build a user/membership management system. 我正在尝试建立一个用户/会员管理系统。 A user will have an expiry date of his membership. 用户将具有其成员的有效期。 My database schema defined like below: 我的数据库架构定义如下:

const membership = new mongoose.Schema({
    UserId:{ type: String, index: {unique: true} },
    name:String,
    joinDate:Date,
    expiryDate:Date,

})

What I am not sure how to do is: when the current date gets past the expiry date, a function userExpired() will run, which will notify the user, ask him to renew, and block his access to certain resources. 我不确定该怎么做:当当前日期超过到期日期时,将运行一个函数userExpired(),该函数将通知用户,要求其续订并阻止其访问某些资源。 But how to trigger this function? 但是如何触发这个功能呢?

One way I can think of is to: 我能想到的一种方法是:

Use 'cron jobs' in linux, which will constantly run a code at certain interval. 在Linux中使用“ cron作业”,它将以一定间隔不断运行代码。 Every time the code run, it performs query on all the endDate and userid, and then compare one by one (use .map() or .forEach() ). 每次运行代码时,它都会对所有endDate和userid执行查询,然后一个一个地进行比较(使用.map()或.forEach())。 When an expiry date is found, run the function userExpired() on that user. 找到到期日期后,对该用户运行函数userExpired()。

I am not sure if there is another way of doing this. 我不确定是否还有另一种方法。 Eg, if MongoDB can constantly monitor the expiry date, and compare it to current date, and call the function, I feel it's better than such additional 'cron job' in Linux. 例如,如果MongoDB可以不断监视到期日期,并将其与当前日期进行比较,然后调用该函数,那么我认为它比Linux中的此类其他“ cron作业”要好。 Any advice is appreciated! 任何建议表示赞赏!

If and only if are using MongoDB Atlas and not self-deployed MongoDB database, then you can look into stich triggers . 当且仅当使用MongoDB Atlas而不是自部署的MongoDB数据库时,您才可以研究stich触发器

Assuming that you are not using MongoDB Atlas (and you can't or not willing to use it due to some reason): 假设您没有使用MongoDB Atlas(并且由于某些原因您不愿意使用它):

MongoDB does have TTL indexes (time-to-live), but unlike your use case, instead of triggering some function it will remove/delete the document. MongoDB确实具有TTL索引 (生存时间),但是与您的用例不同,它不会触发某些功能,而是会删除/删除文档,而不是触发某些功能。 Moreover, it also works like a cron job. 而且,它也像cron工作一样工作。 The background task that removes expired documents runs every 60 seconds. 删除过期文档的后台任务每60秒运行一次。 One way you can utilize this index for your use case is, by using a Change-Data-Capture platform like Debezium . 您可以在用例中使用此索引的一种方法是使用像Debezium这样的Change-Data-Capture平台。

But the above solution is much more complex to deploy and maintain. 但是上述解决方案的部署和维护要复杂得多。 I'll still suggest you to go with Cron jobs, its much simpler and technically same as MongoDB will be doing the same thing in the background. 我仍然建议您使用Cron作业,它与MongoDB在后台做相同的事情更加简单,技术上也一样。 Just I'll add that you don't need to iterate over all records. 我要补充一下,您不需要遍历所有记录。 Just fetch only those records which have expiry date less than now(). 仅获取那些具有小于now()的到期日期的记录。 While inserting record set expiry date in future. 将来插入记录集的到期日期时。 Eg. 例如。 if you need to expire one record after 1 day, than expiry date should be now() + 1 day. 如果您需要在1天后使一条记录过期,则过期日期应为now()+ 1天。 And don't forgot to add index on expiry date. 并且不要忘记在到期日添加索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM