Includi il $group
fase della pipeline dell'operatore dopo il $project
procedere come segue:
db.profil.aggregate([
{ "$match":{ "typ": "Organisation" } },
{ "$project": {
"fooos": { "$size": "$foos" }
} },
{ "$group": {
"_id": null,
"count": {
"$sum": "$fooos"
}
} }
])
Questo raggrupperà tutti i documenti di input dal precedente $project
stage e applica l'espressione dell'accumulatore $sum
sul fooos
campo all'interno del gruppo per ottenere il totale (usando il tuo ultimo esempio):
Questo può essere fatto anche bypassando il $project
pipeline come:
db.profil.aggregate([
{ "$match": { "typ": "Organisation" } },
{ "$group": {
"_id": null,
"count": {
"$sum": { "$size": "$foos" }
}
} }
])
Risultato
/* 0 */
{
"result" : [
{
"_id" : null,
"count" : 24
}
],
"ok" : 1
}