pythonpython-3.xarangodbpyarango

how to bucket aggregate on arangodb


Imagine i have a Edge Document Like This :

[{
    "_from": "mobiles/12345",
    "_to": "mobiles/54321",
    "type": "call",
},
{
    "_from": "mobiles/54321",
    "_to": "mobiles/32145",
    "type": "sms",
},
{
    "_from": "mobiles/54321",
    "_to": "mobiles/12345",
    "type": "call",
}]

and i need to get a list like this when query on 54321:

{"54321":3, "12345":2,"32145":1}

i tried this but this is not what i'm looking for:

for v,e,p in any "mobiles/54321" docs
COLLECT from = e._from , to = e._to with count into len 

return {from, to, len}

i do this in Elasticsearch very easily with aggs query


Solution

  • You can "unroll" the _from and _to attributes to then group by their union of document keys instead of per unique combination, count how often each key occurs and return an object for each bucket with a dynamic attribute key. An outer MERGE() creates the final object that maps keys to counts:

    RETURN MERGE(
      FOR v,e IN ANY "mobiles/54321" docs
        FOR id IN [e._from, e._to]
          COLLECT key = PARSE_IDENTIFIER(id).key WITH COUNT INTO len
          RETURN { [key]: len }
    )