Javascript 过去7天内创建的记录的吃水线查询?
航行v12.14通过水线连接MongoDB 有没有办法对从当前日期算起的最后7天内创建的所有记录执行查找查询?我试着寻找答案,但我猜我没有找到正确的关键字来找到我需要的答案 例如,下面是我的函数:Javascript 过去7天内创建的记录的吃水线查询?,javascript,sails.js,waterline,sails-mongo,Javascript,Sails.js,Waterline,Sails Mongo,航行v12.14通过水线连接MongoDB 有没有办法对从当前日期算起的最后7天内创建的所有记录执行查找查询?我试着寻找答案,但我猜我没有找到正确的关键字来找到我需要的答案 例如,下面是我的函数: getOpen: function getOpen(req, res) { Ticket.find({ status: "open", open_date: [insert magic here] }).then(function response(findModelResults) {
getOpen: function getOpen(req, res) {
Ticket.find({
status: "open",
open_date: [insert magic here]
}).then(function response(findModelResults) {
res.json(200, findModelResults);
})
.catch(function error(findModelError) {
sails.log.error('TicketController.getOpen', findModelError);
res.json(500, findModelError);
});
}
这适用于拉取所有票据,但我不确定如何仅过滤最近7天的票据。我使用了日期格式。下面的代码片段应该可以工作
getOpen: function getOpen(req, res) {
const date = new Date();
Ticket.find({
status: "open",
open_date: {
'>=': moment(new Date(date.getFullYear(), date.getMonth(), date.getDate() - 7))
.utc()
.toISOString();
}
})
.then(function response(findModelResults) {
res.json(200, findModelResults);
})
.catch(function error(findModelError) {
sails.log.error('TicketController.getOpen', findModelError);
res.json(500, findModelError);
});
}
这将检索开放时间大于24小时*7天168小时的票证
getOpen: function getOpen(req, res) {
var sevenDaysAgo = new Date();
sevenDaysAgo.setTime(new Date().getTime() - (7 * 24 * 3600 * 1000));
Ticket.find({
status: "open",
open_date: {
'>=': sevenDaysAgo
}
})
.then(function response(findModelResults) {
res.json(200, findModelResults);
})
.catch(function error(findModelError) {
sails.log.error('TicketController.getOpen', findModelError);
res.json(500, findModelError);
});
}
我已经在我的应用程序中使用momentJS完成了一些其他任务,所以这是一个简单的实现。非常感谢。