Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

group by day/month and take average of rating on that day/month in mongo

I have this data in mongodb:

[
  {
    "rating": 4,
    "ceatedAt": ISODate("2016-08-08T15:32:41.262+0000")
  },
  {
    "rating": 3,
    "createdAt": ISODate("2016-08-08T15:32:41.262+0000")
  },
  {
    "rating": 3,
    "ceatedAt": ISODate("2016-07-01T15:32:41.262+0000")
  },
  {
    "rating": 5,
    "createdAt": ISODate("2016-07-01T15:32:41.262+0000")
  }
]

I need to take the average of rating for that particular day/month depending upon the filter criteria. if the filter range for date is less than a month then it will be grouped basis on day. if filter range for date is more than a month, then it will be grouped basis on month.

This is what I am kind of expecting

[
  {
    "average": 3.5,
    "ceatedAt": "2016-08-08",
    "month": "August"
  },
  {
    "average": 4,
    "ceatedAt": 2016-07-01,
    "month": "July"
  }
]

How can I do it in mongodb?

like image 332
nirvair Avatar asked Aug 11 '16 13:08

nirvair


1 Answers

To group documents by day/month and return the month key in your output, you need to first $project the key fields to appropriate formats using the Date operators, in particular the $dateToString and $month operators.

This can be done in a $project stage prior to the $group step but not necessary since the $group pipeline accommodates mostly the accumulator operators.

In the preceding $group pipeline, you can the group the documents by the formatted date key, aggregate using the $avg operator and return the month as an integer from the previous pipeline using $first accumulator operator.

Running the following aggregation pipeline should give you the desired result:

db.collection.aggregate([
  { "$group": {
    "_id": { 
        "$dateToString": { "format": "%Y-%m-%d", "date": "$ceatedAt" } 
    },
    "average": { "$avg": "$rating" },
    "month": { "$first": { "$month": "$ceatedAt" } },
  } }
]) 
like image 173
chridam Avatar answered Nov 15 '22 23:11

chridam