2014-11-04 67 views
0

我有用於表示折線圖數據的聚合查詢。MongoDB組的持續時間跨度

這是如下:

[ 
{ 
    "$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按小時,星期和月份進行分組。

現在我想添加AM/PM的折線圖,即從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") } 

回答

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",    
    } 
} 
] 
+0

我認爲它會爲AM/PM而不是AM/PM每個月的每一天組每年,對吧?雙組管道是唯一的選擇? – Ninad 2014-11-05 05:25:06

+0

@Ninad,我說將這些部分插入$ group._id(你可以插入到$ group._id.day),然後你可以在每個月的每個月的每一天工作。如果你只是替換整個$ group._id,那麼它將爲每一天忽略其他選項而進行分組。我沒有重複你所做的。 – Wizard 2014-11-05 05:51:47

+0

@Ninad,我更新了答案。也許這種方式更容易理解。 – Wizard 2014-11-05 06:05:43