Home > database >  Mongo time aggregation group
Mongo time aggregation group

Time:09-29

A set inside the
{
"_id" : ObjectId (" 5 a92b6fee16e064282108f6e "),
"Appid" : "huawei. Hs2,"
"Name", "aaaa",
"CreateDate" : ISODate (" the 2018-02-25 T03:03:37. 312 z ")
}

According to a certain period of time after the search, grouped by time years
The db. The log. Aggregate (
{
$match: {
CreateDate: {$gte: new Date (' 2017-03-13 '), $lte: new Date (' 2018-03-15 ')}
}
},
{
$project: {
TimeFormat: {$dateToString: {format: "% Y - % m", the date: "$createDate"}}
}
},
{
$group: {
TimeFormat: {timeFormat: "$timeFormat"},
TotalReq: {$sum: 1}
}
}

)
Perform no grouping, hope to get this data, but did not come out, who can tell me ask him out on the

{
"TimeFormat" : "2018-02,"
TotalReq: 2
}
{
"TimeFormat" : "2018-03,"
TotalReq: 5
}

CodePudding user response:

The db. The log. Aggregate ([
{$match: {createDate: {$gte: new Date (' 2017-03-13 '), $lte: new Date (' 2018-03-15 ')}}},
{$group: {_id: {createDate: {year: {$year: "$createDate"}, the month: {$month: "$createDate"}}}}}
])
  • Related