MongoDB按持续时间跨度分组

时间:2014-11-04 13:55:53

标签: java mongodb aggregation-framework

我有用于表示折线图数据的聚合查询。

如下:

[
{
    "$match": {
        "Category": {
            "$in": ["Mobile"]
        },
        "StartTime": {
            "$gte": {"$date": "2014-01-29T00:00:00.000Z"},
            "$lt": {"$date": "2014-09-29T00:00:00.000Z"}
        }
    }
},
{
    "$group": {
        "_id": {
            "Category": "$Category", 
            "Country":"$Country",
            "City":"$City",
            "day": {
                "day":{"$dayOfMonth": "$StartTime"},
                "month":{"$month":"$month"},
                "year":{"$year":"$year"}
            }
        },
        "UniqueVisits": {
            "$sum": 1
        },
        "Date": {
            "$first": "$StartTime"
        }
    }
},
{
    "$project": {
        "_id": "$_id.Category",
        "Header": {
            "$concat": [{"$substr": [{"$month": "$Date"}, 0,2]},
                "/",
                {"$substr": [{"$dayOfMonth": "$Date"}, 0, 2]},
                "/",
                {"$substr": [{"$year": "$Date"}, 0, 4]}]
        },
        "Name": {
            "$concat": [
                {
                    "$ifNull": ["$_id.Country","notset"]
                },
                "~",
                {
                    "$ifNull": ["$_id.City","notset"]
                }
            ]
        },
        "UniqueVisits": "$UniqueVisits",            
    }
}
]

工作正常。 同样地,我按小时,周和月分组使用$ hour,$ week,$ month进行分组。

现在我想按上午/下午添加折线图,即0-12和12-24之间的小时持续时间,甚至早上(6-12),下午(12-18),晚上(18-24),夜晚(0-6)

有关如何在上述时间段内实现分组的任何帮助。 提前谢谢。

例如:

数据:

{ "_id" : 1, "Date" : ISODate("2014-10-02T19:44:09Z") }
{ "_id" : 2, "Date" : ISODate("2014-10-02T20:44:09Z") }
{ "_id" : 3, "Date" : ISODate("2014-09-03T20:44:09Z") }
{ "_id" : 4, "Date" : ISODate("2014-09-02T20:44:09Z") }
{ "_id" : 5, "Date" : ISODate("2014-09-03T00:00:00Z") }
{ "_id" : 6, "Date" : ISODate("2014-09-03T07:00:00Z") }
{ "_id" : 7, "Date" : ISODate("2014-09-03T14:00:00Z") }
{ "_id" : 8, "Date" : ISODate("2014-10-02T20:47:09Z") }

我希望每天为AM / PM分组的目标如下:

{ "_id" : "PM", "Count" : 3, "Date" : ISODate("2014-10-02T19:44:09Z") }
{ "_id" : "AM", "Count" : 1, "Date" : ISODate("2014-09-03T00:00:00Z") }
{ "_id" : "AM", "Count" : 4, "Date" : ISODate("2014-09-03T00:00:00Z") }

早上,下午,晚上,晚上如下:

{ "_id" : "Evening", "Count" : 3, "Date" : ISODate("2014-10-02T19:44:09Z") }
{ "_id" : "Evening", "Count" : 1, "Date" : ISODate("2014-09-02T20:44:09Z") }
{ "_id" : "Night", "Count" : 1, "Date" : ISODate("2014-09-03T00:00:00Z") }
{ "_id" : "Morning", "Count" : 1, "Date" : ISODate("2014-09-03T07:00:00Z") }
{ "_id" : "Afternoon", "Count" : 1, "Date" : ISODate("2014-09-03T14:00:00Z") }
{ "_id" : "Evening", "Count" : 1, "Date" : ISODate("2014-09-03T20:44:09Z") }

1 个答案:

答案 0 :(得分:0)

将这些部分插入$group._id的正确位置可能会达到您的目标。

ampm : {
    $cond : {
        "if" : {
            $lt : [ {
                $hour : "$StartTime"
            }, 12 ]
        },
        "then" : "AM", 
        "else" : "PM"
    }
},
segment : {
    $let : {
        "vars" : {
            h : {
                $hour : "$StartTime"
            }
        },
        "in" : {
            $cond : {
                "if" : {
                    $lt: [ "$$h", 6 ]
                },
                "then" : "Night",
                "else" : {
                    $cond : {
                        "if" : {
                            $lt : [ "$$h", 12 ]
                        },
                        "then" : "Morning",
                        "else" : {
                            $cond : {
                                "if" : {
                                    $lt : [ "$$h", 18 ]
                                },
                                "then" : "Afternoon",
                                "else" : "Evening"
                            }
                        }
                    }
                }
            }
        }
    }
}

为了便于理解,我将整个粘贴到这里

[
{
    "$match": {
        "Category": {
            "$in": ["Mobile"]
        },
        "StartTime": {
            "$gte": {"$date": "2014-01-29T00:00:00.000Z"},
            "$lt": {"$date": "2014-09-29T00:00:00.000Z"}
        }
    }
},
{
    "$group": {
        "_id": {
            "Category": "$Category", 
            "Country":"$Country",
            "City":"$City",
            "day": {
                "day":{"$dayOfMonth": "$StartTime"},
                "month":{"$month":"$month"},
                "year":{"$year":"$year"},

                // add this part to group by for each day of each month of each year
                ampm : {
                    $cond : {
                        "if" : {
                            $lt : [ {
                                $hour : "$StartTime"
                            }, 12 ]
                        },
                        "then" : "AM", 
                        "else" : "PM"
                    }
                },
                segment : {
                    $let : {
                        "vars" : {
                            h : {
                                $hour : "$StartTime"
                            }
                        },
                        "in" : {
                            $cond : {
                                "if" : {
                                    $lt: [ "$$h", 6 ]
                                },
                                "then" : "Night",
                                "else" : {
                                    $cond : {
                                        "if" : {
                                            $lt : [ "$$h", 12 ]
                                        },
                                        "then" : "Morning",
                                        "else" : {
                                            $cond : {
                                                "if" : {
                                                    $lt : [ "$$h", 18 ]
                                                },
                                                "then" : "Afternoon",
                                                "else" : "Evening"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


            }
        },
        "UniqueVisits": {
            "$sum": 1
        },
        "Date": {
            "$first": "$StartTime"
        }
    }
},
{
    "$project": {
        "_id": "$_id.Category",
        "Header": {
            "$concat": [{"$substr": [{"$month": "$Date"}, 0,2]},
                "/",
                {"$substr": [{"$dayOfMonth": "$Date"}, 0, 2]},
                "/",
                {"$substr": [{"$year": "$Date"}, 0, 4]}]
        },
        "Name": {
            "$concat": [
                {
                    "$ifNull": ["$_id.Country","notset"]
                },
                "~",
                {
                    "$ifNull": ["$_id.City","notset"]
                }
            ]
        },
        "UniqueVisits": "$UniqueVisits",            
    }
}
]