MongoDB 3.6.18
I have two queries which work…
var q1 = [
{ $unwind: '$resp.'+starCode },
{ $project: { stars: '$resp.'+starCode } },
{ $group: {
_id: 1,
total: { $sum: '$stars' },
count: { $sum: { $cond: [{ $ne: ['$stars',''] },1,0] } },
average: { $avg: '$stars' }
}
}
]
which returns…
[
{
"_id": 1,
"total": 51,
"count": 14,
"average": 3.642857142857143
}
]
And another…
var q2 = [
{ $group: {
_id: { $dateToString: { format: '%H:%M %Y-%m-%d', date: '$date' } },
count: { $sum: 1 }
}
}
]
which returns…
[
{
"_id": "14:58 2020-05-14",
"count": 2
},
{
"_id": "14:46 2020-05-14",
"count": 2
},
etc,etc,etc.
However when I try an combine these ( count of stars in the period ) using…
var q3 = [
{ $unwind: '$resp.'+starCode },
{ $project: { stars: '$resp.'+starCode } },
{ $group: {
_id: { $dateToString: { format: '%H:%M %Y-%m-%d', date: '$date' } },
total: { $sum: '$stars' },
count: { $sum: 1 }
}
}
]
The the _id value becomes NULL and there’s no grouping - ie…
[
{
"_id": null,
"total": 51,
"count": 14
}
]
Any Suggestions welcome.