2

I have documents with an array field and I need to sum the sizes of this array across all documents. I'm using python --> PyMongo.

In a nutshell, applying the aforementioned query to this set of documents should return 6:

{_id: a, array: [a,b]}
{_id: b, array: [c, d, e]}
{_id: c, array: [f]}

Searching around the Internet, I've come up with the following solution:

value = collection.aggregate([{"$unwind": "$array"}, {"$group": {"_id": "null", "total": {"$sum": 1}}}])

It works and I get the number I want in the following way:

count = value['result'][0]['total']

Is it the best query I can do? Is there a more efficient way?

I'm not particularly expert about mongo aggregation framework, so I thought it was better to ask.

Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24

1 Answers1

2

No need to unwind the array just to get the size.

>>> collection.aggregate([{'$group': {'_id': None, 'total': {'$sum': {'$size': '$array'}}}}])
{u'ok': 1.0, u'result': [{u'total': 6, u'_id': None}]}
A. Coady
  • 54,452
  • 8
  • 34
  • 40