Mongodb(v2.4.0)$ match汇总不适用于日期范围 [英] Mongodb (v2.4.0) $match aggregate not working with date range
问题描述
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屋!