admin管理员组文章数量:1279144
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();
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();
Share
Improve this question
asked Mar 31, 2014 at 20:31
ac360ac360
7,83514 gold badges55 silver badges95 bronze badges
1 Answer
Reset to default 9The 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 Date Aggregation
operators.
本文标签:
版权声明:本文标题:javascript - MongoDB Aggregation Framework - How to Match by Date Range, Group By Days, and Return Average For Each Day? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741213710a2359634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论