AQL filter by array of IDs
Asked Answered
M

2

5

If I need to filter by array of IDs, how would I do that using bindings? Documentation does not provide any hints on that.

for c in commit
filter c.hash in ['b0a3', '9f0eb', 'f037a0']
return c
Mer answered 7/11, 2017 at 23:59 Comment(2)
By "using bindings", do you mean how to use bind parameters instead of the array literal ['b0a3', '9f0eb', 'f037a0']?Moorefield
Yes, this is exactly what I meant @CoDEmanX. Something like this for c in commit filter c.hash in @hashes return c or for c in commit filter c.hash in [@hashes] return c For now my work around is #2, but it looks very dirty on the implementation.Mer
A
8

Updating the answer to deal with bindings reference that I missed.

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

The key is that when you send the bind param @hashes, it needs to be an array, not a string that contains an array.

If you use the AQL Query tool via the ArangoDB Admin Tool, make sure you click the "JSON" button in the top right to ensure the parameter hashes has the value
["b0a3", "9f0eb", "f037a0"] and not
"['b0a3', '9f0eb', 'f037a0']"

bind parameter

If you want to send a string as a parameter such as "b0a3","9f0eb","f037a0", so { "hashes": "\"b0a3\",\"9f0eb\",\"f037a0\"" } as bind parameter, then you can split the string into an array like this:

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

This example will take the string @hashes and then SPLIT the contents using "," and " as delimiters. This converts the input variable into an array and then the query works as expected. It will also hit an index on the hash attribute.

The delimiters are enclosed with single quote marks to avoid escaping, which would also be possible but less readable: ["\",\"", "\""]

Note that "," is listed first as delimiter, so that the result of the SPLIT is
[ "", "9f0eb", "b0a3", "f037a0" ] instead of
[ "", ",", "9f0eb", "b0a3", "f037a0" ].

The empty string element caused by the first double quote mark in the bind parameter value, which would make the query return commit records with an empty string as hash, can be eliminated with REMOVE_VALUE.

The recommended way is to pass ["b0a3", "9f0eb", "f037a0"] as array however, as shown at the beginning.

Annulation answered 8/11, 2017 at 1:18 Comment(2)
c.hash is just a string. My query is meant to return commits with hashes from within the array of hashes passed as an input parameter. That's why I referred to bindings, or a use of bind parameters.Mer
Added a picture to show the difference of the JSON in the Web UI. Changed the order of the delimiters in the second example using a string as bind parameter value to avoid an element "," and added REMOVE_VALUE() to get rid of the "" element as well, which could give you different query results. Also, you don't need to escape the double quotes if you use single quotes in AQL, which is far better readable.Moorefield
S
0

like this:

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}
Serration answered 18/12, 2018 at 6:58 Comment(1)
Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. ThanksSilicon

© 2022 - 2024 — McMap. All rights reserved.