如何修复 mongoose "gt" 和 "lt" 不工作
How to fix mongoose "gt" and "lt" not working
我正在尝试使用 mongoose ODM 获取给定日期范围内的所有员工,但似乎找不到方法。
我尝试使用不同的日期格式,但想出了在我的数据库中存储 ISO 日期的方法。现在它以 ISODate("2018-12-23T00:00:00Z")
格式保存和检索日期。
但是,我想要的是让所有员工使用 $gte
和 $lte
给定的日期范围
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.staff_id, date:{$gte:params.frm, $lte:params.to}},callback);
}
这什么也没给,但给了当天签名的所有工作人员
Model.find({date:'2018-12-22'},callback);
那是因为你的参数发送的是日期+时间,而 gte 和 lte 只接受日期。您的日志 console.log(new Date(params.frm).toISOString());
的输出应该显示时间戳
一个愚蠢的问题。对不起..
我实际上发现了它有什么问题..这是一个该死的 TYPO
这是我们可以使用不同方法实现的
由于我们只存储日期,猫鼬会自动将我们提供的所有值转换为 ISODate 格式(日期+时间)。它实际上非常好,因为日期或时间的相似模式。所以我们可以简单地使用我给出的上面的代码它会工作正常
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.id, date:{$gte:params.frm, $lte:params.to}},callback);
}
其实不是staff_id:params.staff_id,我所要做的就是params.id
因为这就是我在 http GET 请求中定义 staff_id 的方式。这是 /staff/:id/:frm/:to
无论如何,我们甚至可以使用 where 来执行此操作以及不同的方法...
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(params.id);
AttendanceStaff.find({date:{$gte:params.frm, $lte:params.to}}).sort({date:-1}).where({staff_id:params.id}).exec(callback);
}
就是这样......
我正在尝试使用 mongoose ODM 获取给定日期范围内的所有员工,但似乎找不到方法。
我尝试使用不同的日期格式,但想出了在我的数据库中存储 ISO 日期的方法。现在它以 ISODate("2018-12-23T00:00:00Z")
格式保存和检索日期。
但是,我想要的是让所有员工使用 $gte
和 $lte
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.staff_id, date:{$gte:params.frm, $lte:params.to}},callback);
}
这什么也没给,但给了当天签名的所有工作人员
Model.find({date:'2018-12-22'},callback);
那是因为你的参数发送的是日期+时间,而 gte 和 lte 只接受日期。您的日志 console.log(new Date(params.frm).toISOString());
的输出应该显示时间戳
一个愚蠢的问题。对不起.. 我实际上发现了它有什么问题..这是一个该死的 TYPO
这是我们可以使用不同方法实现的 由于我们只存储日期,猫鼬会自动将我们提供的所有值转换为 ISODate 格式(日期+时间)。它实际上非常好,因为日期或时间的相似模式。所以我们可以简单地使用我给出的上面的代码它会工作正常
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(new Date(params.frm).toISOString());
AttendanceStaff.find({staff_id: params.id, date:{$gte:params.frm, $lte:params.to}},callback);
}
其实不是staff_id:params.staff_id,我所要做的就是params.id 因为这就是我在 http GET 请求中定义 staff_id 的方式。这是 /staff/:id/:frm/:to
无论如何,我们甚至可以使用 where 来执行此操作以及不同的方法...
/**
* Get all attendance of one member for a specific time frame(a month)
*
*/
module.exports.getAttendanceTimeFrame = function(params,callback){
console.log(params.id);
AttendanceStaff.find({date:{$gte:params.frm, $lte:params.to}}).sort({date:-1}).where({staff_id:params.id}).exec(callback);
}
就是这样......