2013-04-19 30 views
0


我正在使用node.js和mongoose。 我需要每個貓鼬文檔中的數值每24小時增加25,000。節點和Mongodb - 每24小時運行腳本

有沒有更好的辦法比:

thing.lastUpdated = new Date(); 

而且

if(/* check how many days(if any) since lase update */> 0){ 
    for(var i = 0;i<days;i++){ 
     //update value 
    } 
} 

回答

1

根據您的使用情況,你可能計算它基於一個創建日期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); 
});