node.js - Mongoose: Running Scheduled Job Query by Date -
i want create scheduled job patients in hospital. patients informed every month reg_date
.
i'm using new date().getdate()
inside scheduled jobs run @ 8.00 in morning send sms patients. meanwhile, had been using string format date save reg_date
in mongodb. here snippets of mongodb docs :
{ customer: "john", reg_date: "2017-02-17t16:39:26.969z" }
i've ben surfing solutions turns out nothing, decided post myself. here trying :
customer.find({"reg_date.getdate()" : new date(2017, 03, 17).getdate()}) .then(function(data) { (var key in data.length) { sendthesms(key[data]); }; });
e.g: doing "i want every patient register @ 17th day of month , send them sms".
any appreciated. :d
for type of bit complex query need use aggregation method instead regular find method.
$project
project fields, here creating new temporary field day
date of reg_date
. query using new field day , result.
this temp field day
never added schema or model, temp view creating in sql.
here projected customer , day please project fields necessary in result.
function getcustomerlist(day, callback){ customer.aggregate([ { $project:{ "customer": "$customer", //repeat same field want in result "reg_date": "$reg_date", "day":{$dayofmonth:"$reg_date"} //put day of month in 'day' } }, { $match:{ "day": day //now match day incoming day value } }, ], function(err, result){ callback(err, result); }) } getcustomerlist(17, function(err, result){ // call function date want // process err & result here });
result this
[{ "_id" : objectid("571f2da8ca97eb10163e6e17"), "customer" : "john", "reg_date" : isodate("2016-04-17t08:58:16.414z"), "day" : 17 }, { "_id" : objectid("571f2da8ca97eb10163e6e17"), "customer" : "prasanth", "reg_date" : isodate("2016-04-17t08:58:16.414z"), "day" : 17 }]
ignore day
field projected during process...
Comments
Post a Comment