Assume there is the following data in my db and I want to aggregate some data grouped by the first target score value.
{
"_id" : ObjectId("5d7f6a937563a63c1d8b4639"),
"target" : [
{
"score" : 3
},
{
"score" : 2
}
]
},
{
"_id" : ObjectId("5d7f6a937563a63c1d8b4640"),
"target" : [
{
"score" : 1
},
{
"score" : 4
}
]
}
So I'm trying to do this:
data.aggregate(
[
{
$match: { 'target.0.score': { $exists: true } }
},
{
$group: {
_id: '$target.0.score',
Datasets: { $sum: 1 }
}
}
]
)
In my code I'm doing some average calculation, that's why I'm using the aggregate method
The result of the query is
[ { _id: [], Datasets: 2 } ]
But I would expect
[
{ _id: 3, Datasets: 1 },
{ _id: 1, Datasets: 1 }
]
_id
should be the grouped score value with the count of all datasets of this score (and some average calculation for this group)