Junhc

岂止于博客

MongoDB实战技巧之日期分组统计

按日期分组统计
{ "_id" : ObjectId("5e6399cff0e3850da6f9dd22"), "uid" : Timestamp(1583585743, 10), "cip" : "10.224.14.124:57242", "t" : ISODate("2020-03-07T12:55:43.230Z") }
...
db.collection.aggregate([{$project:{day:{$dateToString:{format:"%Y-%m-%d",date:"$t"}}}},{$group:{_id:"$t",number:{$sum:1}}}])
{ "_id" : "2020-03-07", "number" : 275800 }
{ "_id" : "2020-03-06", "number" : 289231 }
{ "_id" : "2020-03-05", "number" : 101843 }