0

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

Devnerd
  • 3
  • 1

1 Answers1

0

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 }
)
CodeManX
  • 11,159
  • 5
  • 49
  • 70
  • Thanks for The answer can i ask for the reason of using [key] instead of key ? – Devnerd Apr 02 '21 at 09:05
  • You said that the desired result would be `{"54321":3, "12345":2,"32145":1}`. Using `[key]` will use the **value** of `key` as attribute name (e.g. `"12345"`), whereas just `key` would name it literally `"key"`. This is also how it works in JavaScript. The attribute key doesn't need to be quoted unless it contains characters that require it (like spaces) and will be used verbatim. The square bracket syntax allows you to use a dynamic expression instead. https://www.arangodb.com/docs/stable/aql/examples-dynamic-attribute-names.html – CodeManX Apr 02 '21 at 23:07