一個集合里面
{
"_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
}
uj5u.com熱心網友回復:
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" }}}}}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/96686.html
標籤:MongoDB
