AQL - 在ArangoDB中查找具有非唯一属性的文档

时间:2017-10-01 17:15:36

标签: arangodb aql

说我有以下文件:

{"_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"}

我想写一个AQL语句,它返回按名称​​分组的文档的键,但仅当名称出现多次时才会返回。

所以我想要的输出是:

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

到目前为止,我已经提出了

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

返回:

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

如何修改AQL命令以仅获取具有两个或更多条目的数组?

2 个答案:

答案 0 :(得分:1)

另一种可能性(由于不涉及子查询,可能会更高效):

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

答案 1 :(得分:0)

解决了它:

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