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
}
...全文
978 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2018-02-26
  • 打赏
  • 举报
回复 1
db.log.aggregate([ {$match: { createDate: { $gte: new Date('2017-03-13'), $lte: new Date('2018-03-15') } }} , {$group:{_id:{createDate:{year: { $year: "$createDate"},month: { $month: "$createDate" }}}}} ])

1,747

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧