Hello,
I have a collection with documents that look like this:
{
Date : xxx,
userId : yyy,
dataType : zzz
value : aaa
}
I am trying to run an aggregate query to output the following:
{
_id : zzz, (this will be unique)
date : xxx, (this is a unique list for each _id)
total : sum(aaa) (one number for each date)
}
Here’s my Stitch function’s aggregation code, the function receives the argument ‘userIds’:
result = collection.aggregate([
{ $match: { userId: { "$in" : userIds } } },
{ $group: { _id : "$dataType",
date: { "$first": "$Date" },
total: { $sum: "$value" }
} },
{ $sort : { _id : 1 } }
])
This currently outputs one date per dataType, but I’d like the results to look like this:
{ _id: dataType1, date: date1, total: 10},
{ _id: dataType1, date: date2, total: 20},
{ _id: dataType2, date: date1, total: 3},
{ _id: dataType2, date: date2, total: 77} etc...
or even like this…
{ _id: dataType1, {date: date1, total: 10}, {date: date2, total: 20}},
{ _id: dataType2, {date: date1, total: 3}, {date: date2, total: 77}} etc...
How can I get this working correctly?
As a bonus, I’d also like the results to be sorted by date within each subgroup of dataType.