使用猫鼬在nodeJS中的cronJobs

时间:2019-11-13 12:22:02

标签: node.js mongodb mongoose

我正在尝试在nodeJS中执行cronJobs,我想做的就是在一天结束时评估文档的记录,并根据某些条件使用猫鼬在数据库中执行更新任务,例如:

我创建了以下任务:

import Product from '../models/product.models';
import cron from 'node-cron';

export async function dailyProductUpdate() {
    const products = await Product.find();

    products.forEach(product => {
        if ( product.type === 'permanent' && product.actualPrice != 0 ) {
            if ( product.sellIn >= 10 ) {
                // actualPrice - 1
                // sellIn - 1
            }

        }
    });

}   

cron.schedule("* * * * * *", () => dailyProductUpdate());

模型是这样的:

{ 
   "message":"all products",
   "products":[ 
      { 
         "_id":"5dcbbd97bb34612ccdeac990",
         "name":"super avance",
         "sellIn":8,
         "startingPrice":45,
         "actualPrice":45,
         "createdAt":"2019-11-13T08:23:51.878Z",
         "updatedAt":"2019-11-13T08:23:51.878Z",
         "__v":0
      },
      { 
         "_id":"5dcbc1bdbb34612ccdeac991",
         "name":"mega cobertura",
         "sellIn":0,
         "startingPrice":180,
         "actualPrice":180,
         "createdAt":"2019-11-13T08:41:33.233Z",
         "updatedAt":"2019-11-13T08:41:33.233Z",
         "__v":0
      },
      { 
         "_id":"5dcbc213bb34612ccdeac992",
         "name":"sega avance",
         "sellIn":20,
         "startingPrice":15,
         "actualPrice":15,
         "createdAt":"2019-11-13T08:42:59.836Z",
         "updatedAt":"2019-11-13T08:42:59.836Z",
         "__v":0
      },
      { 
         "_id":"5dcbc3857a8c3c3004ed7b7e",
         "name":"mega avance",
         "type":"fast",
         "sellIn":20,
         "startingPrice":15,
         "actualPrice":15,
         "createdAt":"2019-11-13T08:49:09.857Z",
         "updatedAt":"2019-11-13T08:49:09.857Z",
         "__v":0
      },
      { 
         "_id":"5dcbc3cb7a8c3c3004ed7b7f",
         "name":"mega finance",
         "type":"normal",
         "sellIn":20,
         "startingPrice":15,
         "actualPrice":15,
         "createdAt":"2019-11-13T08:50:19.187Z",
         "updatedAt":"2019-11-13T08:50:19.187Z",
         "__v":0
      },
      { 
         "_id":"5dcbc4147a8c3c3004ed7b81",
         "name":"mega finance2",
         "type":"permanent",
         "sellIn":20,
         "startingPrice":15,
         "actualPrice":15,
         "createdAt":"2019-11-13T08:51:32.450Z",
         "updatedAt":"2019-11-13T08:51:32.450Z",
         "__v":0
      },
      { 
         "_id":"5dcbc7f75bb96732883db33d",
         "name":"finance elite",
         "type":"permanent",
         "sellIn":20,
         "startingPrice":15,
         "actualPrice":15,
         "createdAt":"2019-11-13T09:08:07.138Z",
         "updatedAt":"2019-11-13T09:08:07.138Z",
         "__v":0
      }
   ]
}

但是我不知道如何减少它:

实际价格-1 sellIn-1

0 个答案:

没有答案
相关问题