MongoDB Aggregation Framework - How to Match by Date Range, Group By Days, and Return Average For Each Day?
-
I'm just learning about MongoDB's Aggregation Framework, and I'm wondering if someone can help me improve this query to do the following: Find/Match Records with Dates in between a submitted range Group the results by Day Return Averages for each Day Here is my model, there are some helpful properties to make writing this query this easier, like the day property... // Model var PriceHourlySchema = new Schema({ created: { type: Date, default: Date.now }, day: { type: String, required: true, trim: true }, hour: { type: String, required: true, trim: true }, price: { type: Number, required: true }, date: { type: Date, required: true } }, { autoIndex: true }); Here is my query so far, this only returns a Total Average for all dates included within the range, and does not group by days and return averages for each day, so you can $group by day... var start = moment.utc(req.query.start).startOf('year').toDate(); var end = moment.utc(req.query.start).add('years',1).startOf('year').add('hours',1).toDate(); PriceHourly.aggregate([ { $match: { date: { $gt: start, $lt: end } } }, { $group: { _id: null, price: { $avg: '$price' } } } ], function(err, results){ console.log(results); }); // PriceHourly();
-
Answer:
The problem seems to be with the _id you are using for $group. You should group by $day rather than null, if you want to group the results by day. Try this: PriceHourly.aggregate([ { $match: { date: { $gt: start, $lt: end } } }, { $group: { _id: "$day", price: { $avg: '$price' } } } ], function(err, results){ console.log(results); }); That said, you do not need to store the day and hour as separate elements in the document. It's redundant data. You can extract the day and hour from date field in the aggregation query using the http://docs.mongodb.org/manual/reference/operator/aggregation-date/ operators.
ac360 at Stack Overflow Visit the source
Related Q & A:
- How To Match Clothes?Best solution by Yahoo! Answers
- How to Convert Json date string to more readable date format?Best solution by SharePoint
- How to match a drawn Chinese character to a database of stroke animations?Best solution by Signal Processing
- How to store global date time and convert it to local date time?Best solution by Stack Overflow
- How to match Android Camera Preview correctly?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.