2015-12-02 36 views
0

我保留一組事件,按天計算。如果事件發生一次,那麼它就是'hit'(billState),這是每個位置和材料類別。我很難將數據恢復到需要的地步,我已經嘗試了幾處在這裏找到的例子,還有很多在Mongo Docs中..通常只會得到我需要的部分內容。根據日期和其他字段彙總來自Mongo收集的數據,並計數

樣品我收藏的是這樣的:

{ 
    "_id" : ObjectId("565ca8678e000995a09d1540"), 
    "company" : "someCompany", 
    "location" : "123", 
    "materialCode" : "MATCODE", 
    "materialClass" : "Class", 
    "totalCount" : 8, 
    "billState" : 1, 
    "eventTime" : ISODate("2015-11-30T19:49:59.243Z") 
} 

{ 
    "_id" : ObjectId("565ca9778e000995a09d1541"), 
    "company" : "someCompany", 
    "location" : "1", 
    "materialCode" : "WTHFA", 
    "materialClass" : "OtherClass", 
    "totalCount" : 16, 
    "billState" : 1, 
    "eventTime" : ISODate("2015-11-30T19:54:31.695Z") 
} 

{ 
    "_id" : ObjectId("565ca9778e000995a09d1541"), 
    "company" : "someCompany", 
    "location" : "12345", 
    "materialCode" : "WTHFA", 
    "materialClass" : "thirdClassOfMat", 
    "totalCount" : 16, 
    "billState" : 1, 
    "eventTime" : ISODate("2015-11-30T19:54:31.695Z") 
} 

我能有幾個位置& materialClasses,我只是想算,如果「billState」是一個(簡單,因爲它不會在集合中,否則)。我需要按周,地點,日期,材料類,以打破它..像這樣..

week1 mon tue wed thur fri sat sun 
      ---------------------------- 
location 1 - - - Class otherClass = 2 
location 123- - - Class otherClass = 2 
    week2 mon tue wed thur fre sat sun 
      ---------------------------- 
locations material billState Count = X 
             ----- 
             month total 

目前我只能拿這個,主要是基於(在這一點上)上的另一SO後:

{ 
    "_id" : 12, 
    "weeks" : { 
     "week" : 48, 
     "total" : 6, 
     "days" : [ 
      { 
       "day" : ISODate("2015-12-02T00:00:00.000Z"), 
       "total" : 1 
      }, 
      { 
       "day" : ISODate("2015-12-01T00:00:00.000Z"), 
       "total" : 1 
      }, 
      { 
       "day" : ISODate("2015-11-30T00:00:00.000Z"), 
       "total" : 4 
      } 
     ] 
    }, 
    "monthTotal" : 6 
} 

這是我現在有..

myCollection.aggregate([ 

     // then total per day. Rounding dates 
     { "$group": { 
      "_id": { 
       "$add": [ 
        { "$subtract": [ 
         { "$subtract": [ "$eventTime", new Date(0) ] }, 
         { "$mod": [ 
          { "$subtract": [ "$eventTime", new Date(0) ] }, 
          1000 * 60 * 60 * 24 
         ]}       
        ]}, 
        new Date(0) 
       ] 
      }, 
      "week": { "$first": { "$week": "$eventTime" } }, 
      "month": { "$first": { "$month": "$eventTime" } }, 
      "total": { "$sum": "$billState" } 
     }}, 

     // Then group by week 
     { "$group": { 
      "_id": "$week", 
      "month": { "$first": "$month" }, 
      "days": { 
       "$push": { 
        "day": "$_id", 
        "total": "$total" 
       } 
      }, 
      "total": { "$sum": "$total" } 
     }}, 

     // Then group by month 
     { "$group": { 
      "_id": "$month", 
      "weeks": { 
       "$push": { 
        "week": "$_id", 
        "total": "$total", 
        "days": "$days" 
       } 
      }, 
      "monthTotal": { "$sum": "$total" } 
     }}, 

     {"$unwind": "$weeks"}, 

     { $out : "billingTotals" } 
    ]); 
}; 

我一直在使用更$組,$匹配嘗試與$項目搞砸,但似乎無法得到它按日期細分遠遠不夠幷包括位置。真的,我只需要計算每天的事件,地點和材料類別,然後每週和每月總結一次。因此,在任何特定的日子裏,在一個地方可能會有20個材料類別的點擊,另一個地點的「X」點數量等。我只會計算每個班級每天,每個地點一次點擊。

編輯:輸出 示例(我認爲這是一個好主意..,它是一個漫長的一天)

{ 
    "month" : 12 { 
     "week" : 49 { 
      "day" : 3 { 
       "location": "123", 
       "materials": [ 
           { 
            "class": "materialClass", 
            "total" : 2 
           }, 

           { 
            "class": "otherMatClass", 
            "total" : 5 
           } 
          ], 
       "location": "1234", 
           "materials": [ 
           { 
            "class": "materialClass", 
            "total" : 2 
           }, 

           { 
            "class": "otherMatClass", 
            "total" : 5 
           } 
          ], 

      }, 
      "day" : 4 { 
       "location": "123", 
       "materials": [ 
           { 
            "class": "materialClass", 
            "total" : 2 
           }, 

           { 
            "class": "otherMatClass", 
            "total" : 5 
           } 
          ] 
      } 

     }, 
     "week" : 50 { 
      "day" : 3 { 
       "location": "123", 
       "materials": [ 
           { 
            "class": "materialClass", 
            "total" : 2 
           }, 

           { 
            "class": "otherMatClass", 
            "total" : 5 
           } 
          ] 

      } 
     } 


    } 
} 
+0

請包括樣品JSON證明你正在尋找的結果。 – homam

+0

我加了一個我認爲應該看起來的例子..謝謝! – rhaag71

回答

0

它已經有一段時間,因爲我已經在MongoDB中使用了聚合方法。我把它和你的模擬數據放在一起。不幸的是沒有得到確切的結果;也許它仍然有幫助。祝你好運。

db.materials.aggregate([ 
    { 
     $match: {billState:{$ne:0}} 
    }, 
    { 
     $group: { 
     _id:{location:"$location",date:{week:{$week:"$eventTime"},month:{$month:"$eventTime"},day:{$dayOfMonth:"$eventTime"},year:{$year:"$eventTime"}}}, 
     materials:{$addToSet:{code:"$materialCode",class:"$materialClass"}}, 
     } 
    }, 
    { 
     $sort:{"_id.date": 1} 
    }, 
    { 
     $group: { 
     _id:{date:"$_id.date"}, 
     locations: {$addToSet: {location:"$_id.location",materials:"$materials"}}, 
     } 
    }, 
    { 
     $project:{_id:0, date:"$_id.date",locations:1} 
    } 
]).pretty() 

結果查詢返回

{ 
    "locations" : [ 
     { 
      "location" : "123", 
      "materials" : [ 
       { 
        "code" : "MATCODE", 
        "class" : "Class" 
       } 
      ] 
     }, 
     { 
      "location" : "1", 
      "materials" : [ 
       { 
        "code" : "WTHFA", 
        "class" : "OtherClass" 
       } 
      ] 
     }, 
     { 
      "location" : "12345", 
      "materials" : [ 
       { 
        "code" : "WTHFA", 
        "class" : "thirdClassOfMat" 
       }, 
       { 
        "code" : "WTHFC", 
        "class" : "thirdClassOfMatter" 
       } 
      ] 
     } 
    ], 
    "date" : { 
     "week" : 48, 
     "month" : 11, 
     "day" : 30, 
     "year" : 2015 
    } 
} 
+0

非常接近我所需要的和接近我所嘗試的。謝謝! – rhaag71