i'm trying aggregate data day can render in chart. i've managed grouping $year
, $month
, , $dayofmonth
. however, means date split 3 parts in final result. there way concat 3 numbers date format or there way group day doesn't split date? below working example of have:
sentiment.aggregate([ { $match: { 'content.term' : term_id } }, { $group: { _id: { year : { $year : '$created_at' }, month : { $month : '$created_at' }, dayofmonth : { $dayofmonth : '$created_at' }, }, sum : { $sum : '$score'}, count : { $sum : 1 } }, }, { $project: { _id : 0, date : '$_id', sum : 1, count : 1, avg : { $divide: ['$sum', '$count'] } } } ], function(err, result){ if(err) callback(err); else callback(null, result); });
and here sample result:
[ { "sum": 201, "count": 3, "date": { "year": 2013, "month": 7, "dayofmonth": 5 }, "avg": 67 }, { "sum": 186, "count": 6, "date": { "year": 2013, "month": 7, "dayofmonth": 8 }, "avg": 31 }, { "sum": 834, "count": 9, "date": { "year": 2013, "month": 7, "dayofmonth": 9 }, "avg": 92.66666666666667 } ]
ideally, i'd have date
valid date don't have convert later. i've tried using $concat
works strings. using mongoose
if makes difference.
assuming that, grouping documents year, month , day, hours , minutes useless, can use 1 of operators date sample: $first
, $last
, $min
or $max
.
sentiment.aggregate([ { $match: { 'content.term' : term_id } }, { $group: { _id: { year : { $year : '$created_at' }, month : { $month : '$created_at' }, dayofmonth : { $dayofmonth : '$created_at' }, }, dt_sample : { $first : '$created_at' }, sum : { $sum : '$score'}, count : { $sum : 1 } }, }, { $project: { _id : 0, date : '$dt_sample', sum : 1, count : 1, avg : { $divide: ['$sum', '$count'] } } } ], function(err, result){ if(err) callback(err); else callback(null, result); });
you'll have date field arbitrary hour, minute , seconds.
Comments
Post a Comment