如何统计MongoDB中日期字段上的文档数 脚本:考虑一下,我在MunGdB:中有以下集合 { "_id" : "CustomeID_3723", "IsActive" : "Y", "CreatedDateTime" : "2013-06-06T14:35:00Z" }

如何统计MongoDB中日期字段上的文档数 脚本:考虑一下,我在MunGdB:中有以下集合 { "_id" : "CustomeID_3723", "IsActive" : "Y", "CreatedDateTime" : "2013-06-06T14:35:00Z" },mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,现在我想知道在特定日期(比如2013-03-04)创建文档的数量 因此,我试图使用聚合框架找到解决方案 信息: 到目前为止,我已生成以下查询: collection.aggregate([ { $group: { _id: '$CreatedDateTime' } }, { $group: { count: { _id: null, $sum: 1 }

现在我想知道在特定日期(比如2013-03-04)创建文档的数量 因此,我试图使用聚合框架找到解决方案

信息: 到目前为止,我已生成以下查询:

    collection.aggregate([
        { $group: {
            _id: '$CreatedDateTime'
            }
        },
        { $group: {
            count: {  _id: null, $sum: 1 }
            }
        },
        { $project: {
            _id: 0,
            "count" :"$count"
            }
        }
    ])
问题:现在考虑上述问题,它会给我计数。但不仅仅是基于日期!它需要时间,以及考虑到独特的计数


问题:考虑到该字段有ISO日期,有人能告诉我如何仅根据日期(即不包括时间)计算文档数吗?

将您的两个组替换为

{$project:{day:{$dayOfMonth:'$createdDateTime'},month:{$month:'$createdDateTime'},year:{$year:'$createdDateTime'}}},
{$group:{_id:{day:'$day',month:'$month',year:'$year'}, count: {$sum:1}}}

您可以在此处阅读有关日期运算符的更多信息:

因为它为我提供了
{id:{day:'25',month:'05',year:'2013'},count:31},{id:{day:'26',month:'06',year:'2013'},count:23},
。我还需要使用
{$group:{{u id:null,count:{$sum:1}}}
进行第二组。。谢谢你抽出时间。。