javascript
db.getCollection("CommonStatisticsLog").aggregate([
{
"$match": {
"CreateTime": {
"$gte": ISODate("2025-02-13T09:00:00Z"),
"$lte": ISODate("2025-02-13T09:59:59Z")
},
"NeedStatisticsHourLog": true
}
},
{
"$group": {
"_id": {
"ProductKey": "$ProductKey",
"DeviceName": "$DeviceName",
"LogType": "$LogType",
"StatisticsType": "$StatisticsType"
},
"TotalTimes": { "$sum": "$Times" },
"TotalSeconds": { "$sum": "$Seconds" }
}
},
{
"$project": {
"_id": 0,
"ProductKey": "$_id.ProductKey",
"DeviceName": "$_id.DeviceName",
"LogType": "$_id.LogType",
"StatisticsType": "$_id.StatisticsType",
"DateCode": 2025021309,
"TotalTimes": "$TotalTimes",
"TotalSeconds": "$TotalSeconds"
}
},
{
"$sort": { "CreateTime": -1 }
},
{
"$skip": 0
},
{
"$limit": 1
}
])
解释:
1、aggregate : 开启一个聚合查询;里面包含多个管道列表用 [ ] 包住;
每个管道用 {} 包裹;
2、$match:条件查询,只能出现一次,每个条件用 { }包裹;
javascript
"$match": {
"CreateTime": {
"$gte": ISODate("2025-02-13T09:00:00Z"),
"$lte": ISODate("2025-02-13T09:59:59Z")
},
"NeedStatisticsHourLog": true
}