Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 日期范围为的Mongo聚合是否已中断?_Mongodb_Aggregation Framework - Fatal编程技术网

Mongodb 日期范围为的Mongo聚合是否已中断?

Mongodb 日期范围为的Mongo聚合是否已中断?,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有一个文档集合,其中包含一个字符串字段“action”和一个“receivedTimestamp”日期字段,所有这些字段的值都来自2016年3月和4月。我试图计算在一个日期范围内每个动作的数量 当我运行此命令时: db.getCollection('foo').aggregate([ {"$group": {"_id": "$action", "count": {"$sum": 1}}} ]) db.getCollection('foo').aggregate([ {"$matc

我有一个文档集合,其中包含一个字符串字段“action”和一个“receivedTimestamp”日期字段,所有这些字段的值都来自2016年3月和4月。我试图计算在一个日期范围内每个动作的数量

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到68个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到68个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到68个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到0个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到0个结果


所以我真的开始认为Mongo聚合的日期范围被打破了。我错了吗?

通过切换到ISODate解决了这个问题:

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到68个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到0个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到0个结果

当我运行此命令时:

db.getCollection('foo').aggregate([
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new Date("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$gte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2016-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
db.getCollection('foo').aggregate([
  {"$match": {"receivedTimestamp": {"$lte": new ISODate("2017-01-01")}}}, 
  {"$group": {"_id":   "$action", "count": {"$sum": 1}}}
])
我得到68个结果


这是我期望看到的行为。在这种情况下,我不知道有任何文件要求使用ISODate。

难道你的所有文件都有一个
receivedTimestamp
大于
新日期(“2017-01-01”)
?不,它们都是从2016年3月和4月开始的。可怕的标题,在你问如果广泛使用的软件基本上被打破了,你应该考虑这个事实,“广泛使用”。当然,它并没有被破坏,但你的数据很可能被破坏了。所以忘掉68个结果吧,把4个结果作为文档展示给我们,我们可以自己复制。或者高兴地指出你记录的数据到底出了什么问题,当然这才是真正的问题所在。我的数据没有问题。解决方法是使用ISODate。