mongodb 时间聚合分组
临轩 2018-02-25 09:34:05 一个集合里面
{
"_id" : ObjectId("5a92b6fee16e064282108f6e"),
"appid" : "huawei.hs2",
"name" : "aaaa",
"createDate" : ISODate("2018-02-25T03:03:37.312Z")
}
根据某段时间查询后,按时间年月分组
db.log.aggregate(
{
$match: {
createDate: { $gte: new Date('2017-03-13'), $lte: new Date('2018-03-15') }
}
},
{
$project: {
timeFormat: { $dateToString: { format: "%Y-%m", date: "$createDate" } }
}
},
{
$group: {
timeFormat: { timeFormat: "$timeFormat" },
totalReq: { $sum: 1 }
}
}
)
执行没有分组,希望得到 下面这样的数据,但是没有出来,谁能告诉我问他出在那
{
"timeFormat" : "2018-02",
totalReq:2
}
{
"timeFormat" : "2018-03",
totalReq:5
}