ArangoDB query for arrays

I am having a problem requesting ArangoDB in java for the value of arrays. I tried with both String [] and ArrayList, and without success.

My request:

FOR document IN documents FILTER @categoriesArray IN document.categories[*].title RETURN document 

BindParams:

 Map<String, Object> bindVars = new MapBuilder().put("categoriesArray", categoriesArray).get(); 

categoriesArray contains a bunch of strings. I am not sure why it does not return any results, because if I ask with:

 FOR document IN documents FILTER "Politics" IN document.categories[*].title RETURN document 

I get the results I'm looking for. Just not when using an array or array.

I also tried requesting:

 FOR document IN documents FILTER ["Politics","Law] IN document.categories[*].title RETURN document 

to emulate an ArrayList, but this does not return any results. I would query using a bunch of individual strings, but there are too many of them, and I get an error message from the Java driver during a long query using String. So, I have to request the use of Array or ArrayList.

Array Categories Example:

 ["Politics", "Law", "Nature"] 

Example database image:

enter image description here

+5
source share
1 answer

The reason is that the IN operator works by looking for a value on the left side in each member of the array on the right side.

With the following query, this will work if the "Policy" is a member of document.categories[*].title :

 FOR document IN documents FILTER "Politics" IN document.categories[*].title RETURN document 

However, the following query will not work even if the "Policy" is a member of document.categories[*].title :

 FOR document IN documents FILTER [ "Politics", "Law" ] IN document.categories[*].title RETURN document 

This is because the exact value [ "Politics", "Law" ] will be found in each element on the right, and this will not happen. What you are probably looking for is a comparison that searches for "Politics" and "Law" separately, for example:

 FOR document IN documents LET contained = ( FOR title IN [ "Politics", "Law" ] /* or @categoriesArray */ FILTER title IN document.categories[*].title RETURN title ) FILTER LENGTH(contained) > 0 RETURN document 
+7
source

Source: https://habr.com/ru/post/1211051/


All Articles