查询ArangoDB for Arrays

时间:2015-01-14 06:05:31

标签: arangodb aql

我在使用Java查询ArangoDB中的Arays值时出现问题。我尝试过使用String []和ArrayList,都没有成功。

我的查询:

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

BindParams:

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

categoriesArray包含一堆字符串。我不确定为什么它没有返回任何结果,因为如果我查询使用:

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

我得到了我正在寻找的结果。只是在使用Array或ArrayList时没有。

我也尝试过查询:

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

为了模拟ArrayList,但这不会返回任何结果。我会查询使用一堆单独的字符串,但是有太多的东西,当我用一个很长的字符串查询时,我从Java驱动程序中得到一个错误。因此,我必须使用Array或ArrayList进行查询。

categoriesArray的一个例子:

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

数据库的示例图片:

enter image description here

2 个答案:

答案 0 :(得分:9)

原因是IN运算符的工作原理是在右侧数组的每个成员中搜索其左侧的值。

通过以下查询,如果“政治”是document.categories[*].title的成员,这将有效:

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

然而,即使“政治”是document.categories[*].title的成员,以下内容也无效查询:

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

这是因为将在右侧的每个成员中搜索确切的值[ "Politics", "Law" ],这将不存在。您可能正在寻找的是分别查找"Politics""Law"的比较,例如:

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

答案 1 :(得分:0)

Arango也(现在)有Array Comparison Operators,可以搜索ALL INANY INNONE IN

[ 1, 2, 3 ]  ALL IN  [ 2, 3, 4 ]  // false
[ 1, 2, 3 ]  ALL IN  [ 1, 2, 3 ]  // true
[ 1, 2, 3 ]  NONE IN  [ 3 ]       // false
[ 1, 2, 3 ]  NONE IN  [ 23, 42 ]  // true
[ 1, 2, 3 ]  ANY IN  [ 4, 5, 6 ]  // false
[ 1, 2, 3 ]  ANY IN  [ 1, 42 ]    // true
[ 1, 2, 3 ]  ANY ==  2            // true
[ 1, 2, 3 ]  ANY ==  4            // false
[ 1, 2, 3 ]  ANY >  0             // true
[ 1, 2, 3 ]  ANY <=  1            // true
[ 1, 2, 3 ]  NONE <  99           // false
[ 1, 2, 3 ]  NONE >  10           // true
[ 1, 2, 3 ]  ALL >  2             // false
[ 1, 2, 3 ]  ALL >  0             // true
[ 1, 2, 3 ]  ALL >=  3            // false
["foo", "bar"]  ALL !=  "moo"     // true
["foo", "bar"]  NONE ==  "bar"    // false
["foo", "bar"]  ANY ==  "foo"     // true

所以您现在可以过滤:

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