Node.js Mongoose:按日期运行计划作业查询
我想为医院中的患者创建计划作业。每个月都会通过患者的Node.js Mongoose:按日期运行计划作业查询,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我想为医院中的患者创建计划作业。每个月都会通过患者的reg\u日期通知患者 我在计划的工作中使用new Date().getDate()在早上8点运行,向患者发送短信。同时,我在mongoDB中使用字符串格式的日期保存reg_date。以下是我的mongoDB文档片段: { customer: "John", reg_date: "2017-02-17T16:39:26.969Z" } 我一直在网上寻找解决方案,但结果一无所获,所以我决定发表自己的看法。以下是我试图做的: cu
reg\u日期
通知患者
我在计划的工作中使用new Date().getDate()
在早上8点运行,向患者发送短信。同时,我在mongoDB中使用字符串格式的日期保存reg_date
。以下是我的mongoDB文档片段:
{
customer: "John",
reg_date: "2017-02-17T16:39:26.969Z"
}
我一直在网上寻找解决方案,但结果一无所获,所以我决定发表自己的看法。以下是我试图做的:
customer.find({"reg_date.getDate()" : new Date(2017, 03, 17).getDate()})
.then(function(data) {
for (var key in data.length) {
sendTheSMS(key[data]);
};
});
例如:我所做的是“我想让每个在每月17日登记的患者发短信给他们”
任何帮助都将不胜感激D使用大于和小于
var previousDate =new Date(2017, 1, 16); //month starts with 0
var nextDate=new Date(2017, 1, 18);
customer.find({reg_date : { $gt:previousDate,$lt:nextDate}})
.then(function(data) {
for (var key in data.length) {
sendTheSMS(key[data]);
};
});
对于这种类型的位复杂查询,您需要使用聚合方法,而不是常规的find方法
$project
这将帮助您投影字段,这里我们创建一个新的临时字段day
,只包含reg\u日期的日期。然后我们使用新字段day进行查询,得到结果
这个临时字段day
永远不会添加到您的模式或模型中,它就像我们在SQL中创建的临时视图一样
在这里,我只预测了customer和day,但请预测结果中所有必要的字段
function getCustomerList(day, callback){
customer.aggregate([
{
$project:{
"customer": "$customer", //repeat the same for all field you want in result
"reg_date": "$reg_date",
"day":{$dayOfMonth:"$reg_date"} //put day of month in 'day'
}
},
{
$match:{
"day": day //now match the day with the incoming day value
}
},
], function(err, result){
callback(err, result);
})
}
getCustomerList(17, function(err, result){ // call the function like this with date you want
// Process the err & result here
});
结果是这样的
[{
"_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
}]
忽略过程中投影的day
字段…字符串中的reg\u date
,您无法查询月的哪一天,因为它仅适用于。我建议首先用脚本转换所有文档中reg\u date
中的字符串
那么下面的查询应该可以工作了
customer.aggregate([
{
$project:{
"document": "$$ROOT", //to get the whole document
"day":{$dayOfMonth:"$date"} //put day of month in 'day'
}
},
{
$match:{
"day": 17 //match 17
}
},
], function(data) {
for (var key in data.length) {
sendTheSMS(key[data]);
};
})
自从<代码> ReGrimeDe>代码存储为一个字符串,而不是一个日期/ISODATE,你对你可以运行哪种类型的查询是有限的(所以我同意在其他答案中的一个注释,你应该考虑把它们转换成适当的等量)。
考虑到要查询具有特定月份日期的条目的日期字符串,可以使用以下查询:
或者,动态地:
let today = new Date();
let dom = ('00' + today.getDate()).slice(-2); // zero-pad day of month
let re = new RegExp('-' + dom + 'T');
customer.find({ reg_date : re })
您还应该阅读有关速度优化的内容,但regex查询速度不是很快。谢谢您的回复。尽管如此,我还是希望约翰在我进入17号的时候再次出现,不管是哪一个月或哪一年。我不能用那个查询得到一个。:)谢谢你的回复。尽管如此,我还是希望约翰在我进入17号的时候再次出现,不管是哪一个月或哪一年。我不能用那个查询得到一个。:)安装了什么版本的mongodb?mongodb shell版本:3.2.11答案对您有帮助?感谢您的详细解释,它现在可以工作了。我首先花了时间研究了什么是聚合
和$project
,以便下次使用它们。干杯,巴德!嗨,塔哈,你的回答在我看来很有希望。我还是mongoDB和mongoose ODM的新手。你能详细说明一下$project
部分吗?干杯。它在聚合管道中使用。官方定义很有帮助:“将带有请求字段的文档传递到管道中的下一个阶段。”
let today = new Date();
let dom = ('00' + today.getDate()).slice(-2); // zero-pad day of month
let re = new RegExp('-' + dom + 'T');
customer.find({ reg_date : re })