经过一段时间后,在文档中更改变量值? [英] Change variable value in document after some time passes?
问题描述
我对网络开发很陌生,我想学习新东西。我有一个mongoose架构(我不会发布它,因为它是用我自己的语言,你不会理解,所以我会解释)。
所以我有类似名称,代码,用户创建条目的内容等等(与我的问题无关)。
我有一个变量,比方说,像这样的日期:类型:日期,默认:Date.now
。还有一个,已停用:{type:Boolean,默认值:false}
。
我希望,对于每个条目,在4周后将停用的变量传递给true。
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 explain).
So I have things like name, a code, what user created the entry, etc. (irrelevant for my problem).
I have one variable, let's say, Date like this: type:Date, default: Date.now
. There is another one, deactivated: {type: Boolean, default: false}
.
I want, for each entry, after 4 weeks pass the deactivated variable to change to true.
我不知道该怎么做。我没有尝试任何东西,我在谷歌搜索但我找不到任何东西。
I have no idea how to do that. I didn't try anything and I searched on Google but I couldn't find anything.
我希望你能给我这个想法,让我明白我是怎么做的应该这样做,然后我会尝试自己构建它。
I'd like you to give me the idea and make me understand how I should do it, and then I will try to build it myself.
推荐答案
你所要求的基本上不能像你有两种方法去做这条路线。
What you are asking basically can't be done just like that you have 2 ways about going this route.
- 每次有条目检索时,你都可以运行一个mongoose钩子,例如
pre('find')
将检查4周是否已经过去,如果是这样,然后将停用的更改为true
您可以在这里
- 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 totrue
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'));
}
});
- 你可以每天运行一次cron作业或者两次(取决于你的时间段)继续检查通过创建4周的记录并将
停用
设置为true
- 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
totrue
这是一个很好的cron包 cron
This is a good cron package cron
编辑:cron作业可以是任何时段,每10分钟,1小时,2,100等。 。
The cron job can be any period, every 10 minutes, 1 hour, 2, 100, etc...
祝你好运
这篇关于经过一段时间后,在文档中更改变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!