Maarduk
Maarduk

Reputation: 266

Agreggation with mongodb

Heya I know I maybe shouldnt write here about such an issue but I just cant wrap my head around this. I have mongodb collection, let's call it summary. In there I got following fields: id, subsummaryId, date, price. Subsummaries are in intervals of 1 minute. I want to write aggregationbuilder that would make a report from the existing subsummaries. Report would return average hourly price for each hour per subsummaryId for last 24h. I'm having an issue with aggregation by hour.

db.Report.aggregate([{ $match: { date: { $gt: 0, $lt: 20 }}}, {$group: { _id: "$subsummaryId", hour: {"$hour": "$date"}, price: {$avg: "$price" }}}])

I'm getting

"unknown group operator '$hour'"

Cheers!

Upvotes: 0

Views: 59

Answers (1)

D. SM
D. SM

Reputation: 14490

Both id and hour need to be part of the bucket. Try:

  db.Report.aggregate([{ $match: { date: { $gt: 0, $lt: 20 }}},
 {$group: { _id: {subsummary: "$subsummaryId", hour: {"$hour": "$date"}}, price: {$avg: "$price" }}}])

If this doesn't work, add some sample data to the question.

If your date field contains a timestamp, $lt: 20 doesn't look to me like it's going to do what you want.

Upvotes: 1

Related Questions