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
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']"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With