简体   繁体   中英

AQL - Find documents with nonunique attributes in ArangoDB

Say I have the following documents:

{"_key": "1", "name": "George Washington"}
{"_key": "2", "name": "George Washington"}
{"_key": "3", "name": "John Adams"}
{"_key": "4", "name": "Thomas Jefferson"}
{"_key": "5", "name": "George Washington"}
{"_key": "6", "name": "Thomas Jefferson"}

I want to write an AQL statement that returns the keys of the document grouped by name, but only if the name occurs more than once.

So my desired output is:

[["1", "2", "5"], ["4", "6"]] 

So far I have come up with

FOR doc IN documents
    LET key = doc._key
    COLLECT name = doc.name INTO groups KEEP key
    RETURN (FOR g IN groups RETURN g["key"])

This returns:

[["1", "2", "5"], ["3"], ["4", "6"]]

How can I modify the AQL command to only get arrays with two or more entries?

Another possibility (potentially a bit more efficient as no subquery is involved):

FOR doc IN documents
  LET key = doc._key     
  COLLECT name = doc.name INTO groups KEEP key 
  LET keys = groups[*].key 
  FILTER LENGTH(keys) > 1 
  RETURN keys

Solved it:

FOR doc IN documents
    LET key = doc._key
    COLLECT name = doc.name INTO groups KEEP key
    LET groups2 = (FOR group IN groups RETURN group["key"])
    FILTER LENGTH(groups2) >= 2
    RETURN groups2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM