答案 0 :(得分:0)
您可以使用规则npm包https://www.npmjs.com/package/node-rules来执行此操作。 您可以定义规则,如果执行规则,则后果将删除文档。如果规则是从文件的创建日期计算天数 var RuleEngine = require(“node-rules”);
/* Creating Rule Engine instance */
var R = new RuleEngine();
/* Add a rule */
var rule = {
"condition": (R) => {
console.log(this);
// check if a document creation date and current date , dates >= 7
// and check for other condition
},
"consequence": (R) => {
// delete the document
// if above condition met
}
};
答案 1 :(得分:0)
你需要cron的工作才能实现这一目标 尝试这样的事情(我正在使用node-cron,但你可以使用你想要的任何cron作业库)
import cron from 'node-cron'
import Collection from 'models/YourCollection'
cron.schedule('0 12 * * * *', () => { // execute everyday at 12:00
const lastWeek = new Date();
lastWeek.setDate(lastWeek.getDate() -7);
Collection.deleteMany({'created_at': {'$lte': lastWeek}}) // created more than 7 days
});