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

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

问题描述

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

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

如果我删除$ date block并将其替换为ISOdate函数和日期字符串,那么它似乎工作。我不明白为什么它不工作在java($ match JSON - 我已经从eclipse获取尝试在mongo控制台,不工作。)

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

有没有人知道为什么$ date不能使用MongoDB v2.4.0进行聚合?

解决方案

我通过删除& $ $ $ match 中的 $ date
对于您删除 $ date $ gt & $ lte



所以应该看起来像

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


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"}}
    }
}
)

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>

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

解决方案

I got it solved by removing the "" & $ prefix on the $date field of in $match. For you remove the same for $date, $gt & $lte

So that it should look like

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

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

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