0


node.js と mongoose を使用しています。すべてのマングース ドキュメントに、24 時間ごとに 25,000 ずつ増加する数値が必要です。

より良い方法はありますか:

thing.lastUpdated = new Date();

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

2 に答える 2

1

ユースケースによっては、仮想の作成日に基づいて計算できる可能性があります。

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);
});
于 2013-04-19T00:43:45.933 に答える