2016-08-03 78 views
2

我有以下形式的聚集體查詢,而不是例如在一週小時的持續時間如上面這樣做的MongoDB的骨料每兩小時

db.mycollection.aggregate([ 
    { 
    "$match": 
    { 
     "Time": { $gte: ISODate("2016-01-30T00:00:00.000+0000") } 
    } 
    }, 
    { 
    "$group": 
    { 
     "_id": 
     { 
     "day": { "$dayOfYear": "$Time" }, 
     "hour": { "$hour": "$Time" } 
     }, 
     "Dishes": { "$addToSet": "$Dish" } 
    } 
    }, 
    { 
    "$group": 
    { 
     "_id": "$_id.hour", 
     "Food": 
     { 
     "$push": 
     { 
      "Day": "$_id.day", 
      "NumberOfDishes": { "$size":"$Dishes" } 
     } 
     } 
    } 
    }, 
    { 
    "$project": 
     { 
     "Hour": "$_id", 
     "Food": "$Food", 
     "_id" : 0 
     } 
    }, 
    { 
    "$sort": { "Hour": 1 } 
    } 
]); 

0-1,1-2,2-3,3-4,4-5,...,23-24,我希望能夠在兩小時的時間內做到這一點。例如0-2,2-4,4-6,...,22-24。有沒有辦法做到這一點?

回答

4

提示:使用arithmetic aggregation operators$project

比方說,H=floor(hour/2),其中hour是實際小時從單據日期。然後,你可以通過應用$floor$divide運營商在此日期

"H": { $floor: { $divide: [ { "$hour": "$Time" }, 2 ] } } 

這裏H相當於對小時(Hours=[0,2) => H=0Hours=[2,4) => H=1Hours=[22,24) => H=11等)獲得H,您可以將其與

傳遞給 $group階段
$group: { "_id": { "day": { $dayOfYear: "$Time" }, "H": "$H" } } 

然後就可以輸出對特定H小時與

"Hours": [ { $multiply: [ "$H", 2 ] }, { $sum: [ { $multiply: [ "$H", 2 ] }, 2 ] } ] 

鑑於文件

{ "Time" : ISODate("2016-01-30T01:00:00Z"), "Dish" : "dish1" } 
{ "Time" : ISODate("2016-01-30T02:00:00Z"), "Dish" : "dish2" } 
{ "Time" : ISODate("2016-01-30T03:00:00Z"), "Dish" : "dish3" } 
{ "Time" : ISODate("2016-01-30T04:00:00Z"), "Dish" : "dish4" } 
{ "Time" : ISODate("2016-01-30T05:00:00Z"), "Dish" : "dish5" } 
{ "Time" : ISODate("2016-01-30T06:00:00Z"), "Dish" : "dish6" } 
{ "Time" : ISODate("2016-01-30T07:00:00Z"), "Dish" : "dish7" } 
{ "Time" : ISODate("2016-01-30T08:00:00Z"), "Dish" : "dish8" } 
{ "Time" : ISODate("2016-01-30T09:00:00Z"), "Dish" : "dish9" } 

收集和使用一個聚合上

db.mycollection.aggregate([ 
    { 
    "$match": 
    { 
     "Time": { $gte: ISODate("2016-01-30T00:00:00.000+0000") } 
    } 
    }, 
    { 
    "$project": 
    { 
     "Dish": 1, 
     "Time": 1, 
     "H": { $floor: { $divide: [ { $hour: "$Time" }, 2 ] } } 
    } 
    }, 
    { 
    "$group": 
    { 
     "_id": 
     { 
     "day": { $dayOfYear: "$Time" }, 
     "H": "$H" 
     }, 
     "Dishes": { $addToSet: "$Dish" } 
    } 
    }, 
    { 
    "$group": 
    { 
     "_id": "$_id.H", 
     "Food": 
     { 
     "$push": 
     { 
      "Day": "$_id.day", 
      "NumberOfDishes": { $size: "$Dishes" } 
     } 
     } 
    } 
    }, 
    { 
    "$sort": { "_id": 1 } 
    }, 
    { 
    "$project": 
     { 
     "Hours": [ { $multiply: [ "$_id", 2 ] }, { $sum: [ { $multiply: [ "$_id", 2 ] }, 2 ] } ], 
     "Food": "$Food", 
     "_id": 0 
     } 
    } 
]); 

提供結果

{ "Food" : [ { "Day" : 30, "NumberOfDishes" : 1 } ], "Hours" : [ 0, 2 ] } 
{ "Food" : [ { "Day" : 30, "NumberOfDishes" : 2 } ], "Hours" : [ 2, 4 ] } 
{ "Food" : [ { "Day" : 30, "NumberOfDishes" : 2 } ], "Hours" : [ 4, 6 ] } 
{ "Food" : [ { "Day" : 30, "NumberOfDishes" : 2 } ], "Hours" : [ 6, 8 ] } 
{ "Food" : [ { "Day" : 30, "NumberOfDishes" : 2 } ], "Hours" : [ 8, 10 ] } 
+0

我用蒙戈3.0。地板不適合我。有什麼我可以使用的嗎? – Blabber

+0

[this](http://stackoverflow.com/questions/32554156/how-to-aggregate-by-floor-in-mongodb)可能有幫助 – tarashypka