Voeg de $group toe operator pijplijnfase na het $project stap als volgt:
db.profil.aggregate([
{ "$match":{ "typ": "Organisation" } },
{ "$project": {
"fooos": { "$size": "$foos" }
} },
{ "$group": {
"_id": null,
"count": {
"$sum": "$fooos"
}
} }
])
Dit groepeert alle invoerdocumenten van het vorige $project en past de accumulator-uitdrukking $sum . toe op de fooos veld binnen de groep om het totaal te krijgen (met uw laatste voorbeeld):
Dit kan ook gedaan worden door het $project . te omzeilen pijplijn als:
db.profil.aggregate([
{ "$match": { "typ": "Organisation" } },
{ "$group": {
"_id": null,
"count": {
"$sum": { "$size": "$foos" }
}
} }
])
Uitvoer
/* 0 */
{
"result" : [
{
"_id" : null,
"count" : 24
}
],
"ok" : 1
}