【问题标题】:Mean stack | how to schedule mongodb collection action平均堆栈 |如何安排 mongodb 收集操作
【发布时间】:2020-02-16 17:53:06
【问题描述】:

这是我的第一个q!

我正在做 Mean 项目

这是我的收藏

{
"created":"2019-10-7T10:57:29.579Z",
"client_name":"zuzu",
"client_phone":537486255,
"client_create":"2019-10-16T00:00:00.000Z",
"client_net_operators":"partner",
"client_package":"80gb",
"client_monthly_pay":29,
"client_balance":100,
"client_sim_number":241413244314134,
"__v":0,"_id":"5dac49d32517a19b84cb2a66"}

我想做一个服务器端(nodejs)动作 每月从client_create开始(2019-10-16)下一个意思是2019-11-16

client_balance(100) - client_monthly_pay (29)
and update client_balace t (71) 

希望我自己解释清楚!

谢谢 重发

【问题讨论】:

    标签: node.js mongodb express cron


    【解决方案1】:

    您可以使用节点模块 node-schedule 进行调度,请阅读here

    基本上你必须安排你的工作,所以这被称为cronjob

    像这样使用它:

    let schedule = require("node-schedule");
    
    //get the date from DB, (i leave to you)
    var d = new Date(date_from_DB);
    var date = d.getDate();
    var month = d.getMonth();
    var hour = d.getHours();
    var minutes = d.getMinutes();
    
    var scheduledDateString = '* ' + minute + ' ' + hour + ' ' + day + ' ' + '* *';
    /*
      let's say the date is 20 th Oct 2019, 18:10:00, then scheduledDateString will be
      '* 10 18 20 * *' which says, run this cron job (explaining from right to left) 
      any sec, 10 minutes after 6 pm, on 20th of each month, on any day.
    */
    
    let clientCreateTimeStamp = date_from_DB.getTime();
    let currentTimeStamp = new Date().getTime();
    let diff = clientCreateTimeStamp - currentTimeStamp; //(assuming date_from_DB is always a future date)
    
    //this will start cron job after diff secs, and will run on paricular date every month as explained above
    var myCronJob = schedule.scheduleJob({start: diff, rule: scheduledDateString}, function(){
    
        // call your function from here
    
    });
    

    这里,scheduledDateString = '* ' + minute + ' ' + hour + ' ' + day + ' ' + '* *';cron 格式

    cron 格式包括:

    *    *    *    *    *    *
    ┬    ┬    ┬    ┬    ┬    ┬
    │    │    │    │    │    │
    │    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
    │    │    │    │    └───── month (1 - 12)
    │    │    │    └────────── day of month (1 - 31)
    │    │    └─────────────── hour (0 - 23)
    │    └──────────────────── minute (0 - 59)
    └───────────────────────── second (0 - 59, OPTIONAL)
    

    【讨论】:

      【解决方案2】:

      尝试设置操作系统中定义的 CRON 作业。

      【讨论】:

        猜你喜欢
        • 2016-09-11
        • 2014-02-20
        • 1970-01-01
        • 2019-05-25
        • 2019-06-20
        • 2018-11-29
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        相关资源
        最近更新 更多