I am using node.js and mongoose.
I need a numerical value in every mongoose document to increase by 25,000 every 24 hours.
Is There a better way than:
thing.lastUpdated = new Date();
And
if(/* check how many days(if any) since lase update */> 0){
for(var i = 0;i<days;i++){
//update value
}
}
You could use node-cron to schedule an increment job
Depending on your use-cases, you could possibly calculate it based on a create date with a virtual:
var ThingSchema = new Schema({
created: { type: Date, default: Date.now }
});
ThingSchema.virtual('numerical').get(function () {
if (!this.created) return 0;
var delta = (Date.now() - this.created) || 0;
return 25000 * Math.floor(delta / 86400000);
});
// `created` 2 days ago
new Thing({ created: Date.now() - 172800000 }).save(function (thing) {
console.log(thing.numerical);
});