Change variable value in document after some time passes?

后端 未结 2 1754
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 09:45

I\'m quite new to web development and I want to learn new things. I have a mongoose schema (I won\'t post it because it\'s in my own language, you won\'t understand, so I will e

2条回答
  •  深忆病人
    2021-01-26 10:24

    What you are asking basically can't be done just like that you have 2 ways about going this route.

    1. Everytime there is an entry retrieval you can run a mongoose hook such as pre('find') that will check if 4 weeks have passed if so then change the deactivated to true you can learn more about hooks on here

    Something similar to this example - this example was taken from here

    Schema.pre('find', function() {
      if (!this.getQuery().userId) {
        this.error(new Error('Not allowed to query without setting userId'));
      }
    });
    
    1. You can run a cron job every day once or twice (depends on your time period) that keeps checking for records which passed 4 weeks of creation and set deactivated to true

    This is a good cron package cron

    EDIT: The cron job can be any period, every 10 minutes, 1 hour, 2, 100, etc...

    Good luck

提交回复
热议问题