猫鼬日期过滤器 [英] Mongoose Date Filter

查看:55
本文介绍了猫鼬日期过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据存储在 MongoDB 数据库中,我正在使用 Mongoose 查询数据.我正在尝试对我的数据运行日期查询,以从数据库中返回指定数据范围内的对象.

I have data stored in a MongoDB database and I'm using Mongoose to query the data. I'm trying to run date queries against my data to return objects from the database that fall within the specified data-range.

我的 webform 向外部微服务/api 发送 API 请求,该微服务/api 负责查询 Mongo 数据库中的数据.API 接收代表天数的单个值.例如:日期:7d".然后我继续像这样构建猫鼬查询:

My webform sends an API request to an external micro-service/api that is responsible for querying the data in the Mongo database. The API receives a single value that is representative of a number of days. eg: date: "7d". I then proceed to build the mongoose query like so:

if (data.date) {
    const date = new Date();
    const dateRange = data.date.slice(0, -1); // strip the "d" from "7d"
    date.setDate(date.getDate() - dateRange);
    query.start = { $lte: date.toISOString() };
    console.log(query);
}

我数据库中的一个示例日期是:

and an example date from my database is:

开始:2016-02-10T09:09:01.000Z

然后执行查询:

Call.find(query, function (error, docs) {
    if (error) {
        callback(error, null);
    } else {
        callback(null, docs);
    }
});

最后,一个query的例子:

{ approved: 1, start: { '$lte': '2016-02-09T14:24:29.115Z' } }

但是,无论我发送到 API 的日期是什么时候,响应始终为空...

However, no matter what date I send to my API the response is always empty...

推荐答案

使用实际的日期对象进行查询,而不是像您目前所做的那样使用字符串.因为 mongo 存储用 ISODate 包裹的日期 助手和底层 BSON(mongo 本地使用的存储数据格式)有一个专用的日期类型 UTC 日期时间,它是一个 64 位(因此,8 字节)有符号整数,表示自 Unix 时间纪元以来的毫秒数,您的查询不会返回任何内容,因为它会将 mongo 中的日期字段与 ISO 格式的字符串进行比较.

Use the actual date object for your query, not string as you are doing presently. Because mongo stores dates wrapped with the ISODate helper and the underlying BSON (the storage data format used by mongo natively) has a dedicated date type UTC datetime which is a 64 bit (so, 8 byte) signed integer denoting milliseconds since Unix time epoch, your query doesn't return anything as it will be comparing the date fields in mongo with an ISO formatted string.

因此,删除 toISOString() 转换并使用日期对象:

So, drop the toISOString() conversion and use the date object:

if (data.date) {
    const date = new Date();
    const dateRange = data.date.slice(0, -1); // strip the "d" from "7d"
    date.setDate(date.getDate() - dateRange);
    query.start = { $lte: date };
    console.log(query);
}

Call.find(query, function (error, docs) {
    if (error) callback(error, null);
    callback(null, docs);    
});

<小时>

更好的是,您可以使用 momentjs 插件,它具有非常直观和简单的日期时间操作 API.您可以使用的一种方法是 subtract() 函数获取日期对象 n 天前:


Better yet, you can use the momentjs plugin that has a very intuitive and easy datetime manipluation API. One method you can use is the subtract() function to get the date object n number of days ago:

if (data.date) {    
    const dateRange = data.date.slice(0, -1); // strip the "d" from "7d"
    const date = moment().subtract(dateRange, "days");
    query.start = { $lte: date };
    console.log(query);
}

Call.find(query, function (error, docs) {
    if (error) callback(error, null);
    callback(null, docs);    
});

这篇关于猫鼬日期过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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