在Sails.JS Waterline中查询完整日期格式之间使用 [英] Using between query for full date format in Sails.JS Waterline

查看:146
本文介绍了在Sails.JS Waterline中查询完整日期格式之间使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据使用Waterline ORM SailsJS在日期之间进行在水线适配器中查询日期之间的时间,如下所示:

According to Between Dates using Waterline ORM SailsJS it is possible to do a between query on dates in the Waterline adapter like the following:

User.find({
    createdAt: { '>': dateFrom, '<': dateTo }
}).exec(/* ... */);

但是,这仅适用于 2018-04-11
是否可以在此查询中使用完整的ISO8601日期格式:例如 2018-04-11T12:45:00.000Z

However, this does only work with date formats like 2018-04-11. Is it somehow possible to use the full ISO8601 date format in this query: e.g. 2018-04-11T12:45:00.000Z?

编辑:添加了最少的工作示例代码。

readDataInDateRange: function(req, res) {
    var dateFrom = req.param("dateFrom");
    if (dateFrom == null || dateFrom == ''){
        return res.badRequest();
    }
    var dateTo = req.param("dateTo");
    if (dateTo == null || dateTo == ''){
        return res.badRequest();
    }
    User.find({createdAt: { '>=': dateFrom, '<=': dateTo}})
    .exec(function afterwards(err, users) {
        if (err) {
            sails.log(err);
            return res.serverError(err);
        }
        return res.ok(users);
    });
}


推荐答案

如果使用带有 CLI 的mongodb ,实际上可以使用

If you use mongodb with CLI, may practically use

    $gt

运算符,显示所有值特定的值更高。在这里,我们在 sails.js 中具有显示 查询语言 ,并提供详细信息。

operator that show all value grater than specific value. Here we have syntax in sails.js that show Query Language with details.

下面的代码是一个我们可以用来显示具有条件的所有数据的基本示例。

A basic example that we can use to show all data with criteria is something like below code.

    Model.find({ age: { '<=': 20 }})



更大的等于运算符



在您的情况下,我们大多数按dateTime模式进行过滤。

greater equal operators

In your case we most filter by dateTime mode.

    Model.find({
          where: {
            date: {
              '<=': new Date('2018-08-24T14:56:21.774Z')
            }
          }
        });

时间'2018-08-24T14:56:21.774Z'例如,您可以将所有 ISOformat 时间放在此处。

the time '2018-08-24T14:56:21.774Z' is example and you can put all ISOformat time here.

如果您数据库中的数据具有时间戳格式,则需要将查询更改为数字而不是日期时间格式

If your data in database has timestamp format you need to change your queries to number instead of date-time format

    Model.find({
          where: {
            createdAt: {
              '<=': new Date('2018-08-24T14:56:21.774Z').getTime()
            }
          }
        });

日期是全局对象,是可以实例化并创建新时间类的JavaScript 。可以生成带有 .getTime()时间戳的时间戳。

Date is global object is JavaScript that can instantiated and create new time class. with .getTime() timestamp can be generated.

或者如果开发人员希望在两次之间收集数据,我建议使用以下正确的语法。

Or in case that the developer wants to collect data between two times, I suggest using below syntax that is correct.

    Model.find({
          where: {
            updatedAt: {
              '>': new Date('2018-08-21T14:56:21.774Z').getTime(),
              '<': new Date('2018-08-25T14:56:21.774Z').getTime()
            }
          }
        });

全部检查完毕,无需担心校正! ;)

Checked them all and don't need worry about correction! ;)

这篇关于在Sails.JS Waterline中查询完整日期格式之间使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆