How to aggregate by date when a full timestamp is given in aggregation framework?

前端 未结 3 351
[愿得一人]
[愿得一人] 2020-12-08 04:55

I have a collection of errors, so that every error carries a date field. How can I aggregate/count/group the errors by DAY only (i.e. exclude the time of the da

3条回答
  •  Happy的楠姐
    2020-12-08 05:09

    You can convert your timestamp field to string with specific date format by using $project (aggregation)

    aggregate([
        {
            '$project': {
                newFieldName: {'$dateToString': {format: '%Y-%m-%d', date: '$yourDateFieldName'}}
            }
        }, {
            '$group': {
                _id: {newFieldName: '$newFieldName'},
                viewCount: {'$sum': 1}
            }
        }, 
        {'$sort': {'_id.newFieldName': 1}}
    ], {})
    

提交回复
热议问题