Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb sum the size of array fields

Tags:

so i have a bunch of simple documents like

{    "foos": [     ObjectId("5105862f2b5e30877c685c58"),     ObjectId("5105862f2b5e30877c685c57"),     ObjectId("5105862f2b5e30877c685c56"),   ],    "typ": "Organisation",  } 

and i want to find out the overall size of associated foos to documents of type "Organisation"

so i have this aggregate query

db.profil.aggregate(    [    {     $match:{      "typ":"Organisation"     }    },       {          $project: {             fooos: { $size: "$foos" }          }       }    ] ) 

this returns the count of all foos for each document

like :

{ "_id" : ObjectId("50e577602b5e05e74b38a6c8"), "foooos" : 1 } { "_id" : ObjectId("51922170975a09f363e3eef5"), "foooos" : 3 } { "_id" : ObjectId("51922170975a09f363e3eef8"), "foooos" : 2 } { "_id" : ObjectId("5175441d975ae346a3a8dff2"), "foooos" : 0 } { "_id" : ObjectId("5192216f975a09f363e3eee9"), "foooos" : 2 } { "_id" : ObjectId("5192216f975a09f363e3eeeb"), "foooos" : 3 } { "_id" : ObjectId("5192216f975a09f363e3eee4"), "foooos" : 2 } { "_id" : ObjectId("5192216f975a09f363e3eee6"), "foooos" : 2 } { "_id" : ObjectId("5192216f975a09f363e3eedb"), "foooos" : 2 } { "_id" : ObjectId("51922174975a09f363e3ef4a"), "foooos" : 1 } { "_id" : ObjectId("5192216f975a09f363e3eee1"), "foooos" : 1 } { "_id" : ObjectId("5192216e975a09f363e3eed7"), "foooos" : 2 } { "_id" : ObjectId("5192216f975a09f363e3eeee"), "foooos" : 3 } 

is there some query that would return the summed up count for foos of all documents ?

i played arround with $sum but dont know how to combine with my query, i only do get syntax errors, it would be cool to know if this is possible

like image 292
john Smith Avatar asked May 14 '15 11:05

john Smith


People also ask

How do I sum fields in MongoDB?

If used on a field that contains both numeric and non-numeric values, $sum ignores the non-numeric values and returns the sum of the numeric values. If used on a field that does not exist in any document in the collection, $sum returns 0 for that field. If all operands are non-numeric, $sum returns 0 .

How do you sum the value of a key across all documents in a MongoDB collection?

To get sum the value of a key across all documents in a MongoDB collection, you can use aggregate().

What is $size in MongoDB?

The $size operator in MongoDB is used to fetch the document that has an array field of a specific size. The $size only deals with arrays and accepts only numeric values as a parameter.

What is $project in MongoDB?

Definition. $project. Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.


1 Answers

Include the $group operator pipeline stage after the $project step as follows:

db.profil.aggregate([    { "$match":{ "typ": "Organisation" } },    { "$project": {          "fooos": { "$size": "$foos" }    } },    { "$group": {        "_id": null,        "count": {            "$sum": "$fooos"        }    } } ]) 

This will group all the input documents from the previous $project stage and applies the accumulator expression $sum on the fooos field within the group to get the total (using your last example):

This can also be done by-passing the $project pipeline as:

db.profil.aggregate([    { "$match": { "typ": "Organisation" } },    { "$group": {        "_id": null,         "count": {             "$sum": { "$size": "$foos" }         }     } } ]) 

Output

/* 0 */ {     "result" : [          {             "_id" : null,             "count" : 24         }     ],     "ok" : 1 } 
like image 61
chridam Avatar answered Oct 12 '22 13:10

chridam