水线查询最近 7 天内创建的记录?

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 大于 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);
        });
    }