Mongodb (v2.4.0) $match 聚合不适用于日期范围 [英] Mongodb (v2.4.0) $match aggregate not working with date range

查看:30
本文介绍了Mongodb (v2.4.0) $match 聚合不适用于日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mongodb java 驱动程序通过 maven 存储库(如下面的 pom.xml)来查询日期范围与聚合框架之间的交易.java 驱动程序生成以下 $match,我试图在 mongo 控制台上验证它并发现它不起作用:

I am using mongodb java driver thru maven repository (as below in pom.xml) to query transactions between date range with aggregate framwork. The java driver generates following $match that I tried to validate on mongo console and found that it does not work:

db.transactions.aggregate(
{ "$match" : 
    { 
        "created_at" : { "$gt" : { "$date" : "2001-04-12T12:00:00.000Z"} , "$lte" : { "$date" : "2020-04-13T12:00:00.000Z"}}
    }
}
)

如果我删除 $date 块并将其替换为 ISOdate 函数和日期字符串,那么它似乎可以正常工作.我不明白为什么它在 java 中不起作用($match JSON - 我从 eclipse 中获取并在 mongo 控制台中尝试,但它也不起作用.)

If I remove $date block and replace it with ISOdate function and date string then it seem to be working. I failed to understand why it does not work in java ($match JSON - I had fetched from eclipse to try in mongo console and that do not work as well.)

pom.xml
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>2.11.0</version>
</dependency>

有谁知道为什么 $date 不能与使用 MongoDB v2.4.0 的聚合一起工作?

does any one know why $date is not working with aggregate using MongoDB v2.4.0?

推荐答案

在传递给 $match 聚合之前,您必须格式化日期.

You have to format the date before passing onto $match aggregate.

Order.aggregate([
        {
          $match: {
            createdAt: {
              $gte: new Date(req.body.startDate),
              $lt: new Date(req.body.endDate)
            }
          }
        },
        {
          $lookup: {
            from: 'acbinstallerpayments',
            localField: "_id",
            foreignField: 'customerObjectID',
            as: 'installerPaymentDetails'
          }
        }
      ]);

这篇关于Mongodb (v2.4.0) $match 聚合不适用于日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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