I have an aggregation query for MongoDB 2.4 (we can't use another version :( ).
At one point I need the size or count of a set created in a group:
{
$group: {
_id: {
type: '$type',
week: '$week',
day: '$day',
year: '$year',
hour: '$hour'
},
minutes: {
$addToSet: "$minutes"
},
sum_online: {
$sum: "$online"
},
sum_offline: {
$sum: "$offline"
},
sum_expired: {
$sum: "$expired"
},
min_date: {
$min: "$date"
},
max_date: {
$max: "$date"
}
}
},
{
$project: {
day: "$_id.day",
week: "$_id.week",
year: "$_id.year",
type: "$_id.type",
hour: "$_id.hour",
measurements: { $size: "$minutes" },
sum_online: "$sum_online",
sum_offline: "$sum_offline",
sum_expired: "$sum_expired",
min_date: "$min_date",
max_date: "$max_date"
}
},
It's about the "minutes". The result of the addToSet will be:
[
0,
10,
20,
30,
40,
50
]
Those are six items and I need the size of that set in my projection but version 2.4 has no $size function I can use. measurements: { $size: "$minutes" },
doesn't work. I get this error:
Thu Feb 18 12:50:03.214 aggregate failed: {
"errmsg" : "exception: invalid operator '$size'",
"code" : 15999,
"ok" : 0
Does anyone know of a way to get the size of that set?