问题
I have read some solutions and wonder if I can update (not delete) a document in mongodb if the date is expired after 2 days. Like this.
date:2019-02-15
status:not expired
After 2 days update the status into expired. Here is my code for posting a document.
var bloodstock = new Bloodstock();
bloodstock.date = new Date();
bloodstock.status = "not expired";
bloodstock.save(function(err) {});
回答1:
Run a cron at daily midnight to compare the date. If the date exists 2 days update the status on the database.
var CronJob = require('cron').CronJob;
const job = new CronJob('00 00 00 * * *', function () {
if (Date.parse(today_date) > Date.parse(new Date(db_date).getTime() + (2 * 24 * 60 * 60 * 1000))) {
//Update your status
...
}
});
job.start();
Note : Date comparison can be done either on the MongoDB query or Javascript,It depends on the size of document you need to update.
来源:https://stackoverflow.com/questions/54701819/is-there-a-way-to-update-a-document-when-date-is-expired-in-mongodb