Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb aggregate every two hours

I have an aggregate query of the following form

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 }
  }  
]);

Instead of doing this as above in one hour durations e.g. 0-1,1-2,2-3,3-4,4-5,...,23-24, I want to be able to do this in two hour durations. e.g. 0-2,2-4,4-6,...,22-24. Is there a way to do that?

like image 252
Blabber Avatar asked Apr 09 '26 23:04

Blabber


1 Answers

Hint: use arithmetic aggregation operators in $project

Lets say, H=floor(hour/2), where hour is actual hour from document date. Then you can get H by applying $floor and $divide operators to this date

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

Here H corresponds to the pair of hours (Hours=[0,2) => H=0, Hours=[2,4) => H=1, Hours=[22,24) => H=11, etc.) and you can pass it to the $group stage with

$group: { "_id": { "day": { $dayOfYear: "$Time" }, "H": "$H" } }

Then you can output the pair of hours for specific H with

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

Given collection of documents

{ "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" }

and using the next aggregate on it

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
      }
  }
]);

provides the result

{ "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 ] }
like image 142
tarashypka Avatar answered Apr 12 '26 00:04

tarashypka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!