按时间戳分组的猫鼬

时间:2016-01-19 10:31:53

标签: node.js mongoose

我正在为每个文档添加确切的时间戳。我需要根据日期显示收到的文件数量。

我绑了

{$group: {_id: '$timestamp' }},function(err,docs){}

但由于时间戳还包括时间,因此每个文档都在不同的组中。

如何按日期/月份对这些文档进行分组?

1 个答案:

答案 0 :(得分:1)

发现它! 使用日期聚合运算符。 https://docs.mongodb.org/manual/reference/operator/aggregation-date/

DataStream.aggregate(
    {$group:
    {_id: {$hour:'$at'},
        count: { $sum: 1 }
    }},function(err,docs)
    {
        if(err)
            console.log(err);
        res.json(docs);
    });
相关问题