I have documents that looks like that:
ref1: 5ec68d5edcf68a016c4d1f68
ref2: 5ec68d5edcf68a016c4d1f68
country: "US"
date: 2020-05-22T00:00:00.000+00:00
hour: 14
time: 2020-05-22T14:42:11.396+00:00
type: "x"
which I aggregate by type. my desired outcome is something like:
ref1: 5ec68d5edcf68a016c4d1f68
ref2: 5ec68d5edcf68a016c4d1f68
date: 2020-05-22T00:00:00.000+00:00
contry: {
US: {x: 1, y: 10 ...},
CA: ....
.
.
},
hour: {
14: {x: 5, y: 0 ...},
20: ....
.
.
}
or alternatively to create a document for each ‘attribute’:
ref1: 5ec68d5edcf68a016c4d1f68
ref2: 5ec68d5edcf68a016c4d1f68
date: 2020-05-22T00:00:00.000+00:00
aggregatedBy: 'country'
res: {
US: {x: 1, y: 10 ...},
CA: ....
.
.
}
currently I have $group that aggregates only per ref1,ref2,date and looks like:
{
_id: { date : '$date', ref1: '$ref1', ref2: '$ref2' },
x: {
$sum: {$cond: [{$eq: ['$type', 'x']}, 1, 0]}
},
y: {
$sum: {$cond: [{$eq: ['$type', 'y']}, 1, 0]}
},
}
I have no clue on how to apply that deeper level.
Thanks