Mongodb 日期范围为的Mongo聚合是否已中断?
我有一个文档集合,其中包含一个字符串字段“action”和一个“receivedTimestamp”日期字段,所有这些字段的值都来自2016年3月和4月。我试图计算在一个日期范围内每个动作的数量 当我运行此命令时: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
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。