[英]Waterline query for records created within the last 7 days?
Sails v12.14通過Waterline連接到MongoDB
有沒有一種方法可以查詢從當前日期起過去7天內創建的所有記錄? 我嘗試搜索答案,但我猜我沒有找到正確的關鍵字來提出所需的答案。
例如,這是我的功能:
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天進行過濾。
我已經使用momentJS進行日期格式化。 以下代碼段應該有效。
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);
});
}
這將檢索open_date大於24h * 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);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.