|
I have seen a number of customer queries use $addToSet in scenarios that it is not necessary for and is in fact a bad choice due to it's materialization of a giant array. For example:
[
|
{
|
"$match": {
|
"myField.target": "TARGET"
|
}
|
},
|
{
|
"$group": {
|
"_id": {},
|
"value": {
|
"$addToSet": "$metadata.something_id"
|
}
|
}
|
},
|
{
|
"$project": {
|
"_id": 0,
|
"value": {
|
"$size": "$value"
|
}
|
}
|
}
|
]
|
This query only needs the size of the resulting "value" array, so could be re-written like so:
[
|
{
|
"$match": {
|
"metadata.target": "cloud"
|
}
|
},
|
{
|
"$group": {
|
"_id": "$metadata.something_id"
|
}
|
},
|
{
|
"$group": {
|
"_id": {},
|
value: {$sum: 1}
|
}
|
},
|
{
|
"$project": {
|
"_id": 0
|
}
|
}
|
]
|
|