AQL按ID数组过滤

时间:2017-11-07 23:59:34

标签: arangodb aql

如果我需要按ID数组过滤,我该如何使用绑定? 文档没有提供任何暗示。

for c in commit
filter c.hash in ['b0a3', '9f0eb', 'f037a0']
return c

2 个答案:

答案 0 :(得分:8)

更新答案以处理我错过的绑定参考。

LET commit = [
    { name: "111", hash: "b0a3" },
    { name: "222", hash: "9f0eb" },
    { name: "333", hash: "asdf" },
    { name: "444", hash: "qwer" },
    { name: "555", hash: "f037a0" }
]

FOR c IN commit
FILTER c.hash IN @hashes
RETURN c

关键是当你发送绑定参数@hashes时,它需要是一个数组,而不是一个包含数组的字符串。

如果您通过ArangoDB管理工具使用AQL查询工具,请确保单击" JSON"右上角的按钮可确保参数hashes的值为
["b0a3", "9f0eb", "f037a0"]而不是 "['b0a3', '9f0eb', 'f037a0']"

bind parameter

如果要将字符串作为参数(例如"b0a3","9f0eb","f037a0",因此{ "hashes": "\"b0a3\",\"9f0eb\",\"f037a0\"" }作为绑定参数)发送,则可以将字符串拆分为如下数组:

LET commit = [
    { name: "111", hash: "b0a3" },
    { name: "222", hash: "9f0eb" },
    { name: "333", hash: "asdf" },
    { name: "444", hash: "qwer" },
    { name: "555", hash: "f037a0" }
]

FOR c IN commit
FILTER c.hash IN REMOVE_VALUE(SPLIT(@hashes, ['","', '"']), "")
RETURN c

此示例将使用@hashesSPLIT作为分隔符使用",""作为分隔符。这会将输入变量转换为数组,然后查询按预期工作。它还会点击hash属性的索引。

分隔符附有单引号以避免转义,这也可能但不太可读:["\",\"", "\""]

请注意,","首先列为分隔符,因此SPLIT的结果为
[ "", "9f0eb", "b0a3", "f037a0" ]而非 [ "", ",", "9f0eb", "b0a3", "f037a0" ]

由绑定参数值中的第一个双引号引起的空字符串元素,可以使查询返回带有空字符串作为哈希的提交记录,可以使用REMOVE_VALUE消除。

建议的方法是将["b0a3", "9f0eb", "f037a0"]作为数组传递,但如开头所示。

答案 1 :(得分:0)

像这样:

with person FOR id in ["person/4201061993070840084011","person/1001230840198901011999","person/4201008406196506156918"]
FOR v,e,p In 1..1 ANY id
relation_samefamily,stay,relation_internetbar,relation_flight,relation_train
OPTIONS {
    bfs:true 
} 
FILTER (p.edges[*]._from ALL IN  ["person/42010619930708400840084011","person/10012310840989084001011999","person/4201060840196506156918"] and p.edges[*]._to ALL IN  ["person/4201061993070808404011","person/1001231908408901011999","person/4200840106196506156918"]) 
RETURN {v,e}