AQL list operation

652 views
Skip to first unread message

Tomas Bosak

unread,
Jul 13, 2013, 1:19:01 PM7/13/13
to aran...@googlegroups.com
Does AQL support in some way following list operation:

let array = [
    { "foo": [
            { "bar": 1 }, 
            { "bar": 2 }, 
            { "bar": 3 }
        ] 
    },
    { "foo": [
            { "bar": 2 }, 
            { "bar": 3 }
        ] 
    },
    { "foo": [
            { "bar": 3 }
        ] 
    }
]

FOR item IN array
    FILTER { "bar": 2 } && { "bar": 3 } in item.foo
    RETURN item

or the only way I can check for multiple items in a list is to do something like (whether it's && or || operator):

FOR item IN array
    FILTER { "bar": 2 } in item.foo && { "bar": 3 } in item.foo
    RETURN item

Tomas Bosak

unread,
Jul 13, 2013, 3:15:04 PM7/13/13
to aran...@googlegroups.com
Also another case - lets say I want to find out if part of the object is present in array, e.g.:

let items = [
    { "array": [
            { "foo": 1, "bar": 2, "baz": 3 },
            { "foo": 1, "bar": 2 }
            ...
        ]
    },
    ...
]

FOR item IN items
    FILTER {"foo": 1, "bar": 2} in item.array
    RETURN item

what I would like to get are the items which has both "foo" and "bar" attributes with specific values regardless whether item.array contains "baz" or any other attribute.

Jan Steemann

unread,
Jul 15, 2013, 5:59:11 AM7/15/13
to aran...@googlegroups.com, Tomas Bosak
Hi Tomas,

the below way

> FILTER { "bar": 2 } in item.foo && { "bar": 3 } in item.foo

is the right one to use.

Best regards
Jan
> --
> You received this message because you are subscribed to the Google
> Groups "ArangoDB" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to arangodb+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Jan Steemann

unread,
Jul 15, 2013, 6:19:33 AM7/15/13
to aran...@googlegroups.com, Tomas Bosak
The IN operator checks for equality, so it cannot be used if you are
looking for partial matches.

Though probably not an intuitive solution, the following query should work:

LET items = [
{
"array": [
{ "foo": 1, "bar": 2, "baz": 3 },
{ "foo": 1, "bar": 2 }
]
}
]

FOR item IN items
LET contains = (
FOR ae IN item.array
FILTER MATCHES(ae, [ { foo: 1, bar: 2 } ])
LIMIT 1
RETURN 1
)

FILTER LENGTH(contains) > 0
RETURN item


For outer FOR loop will iterate over each element in "items", and for
each outer element there is a sub-query over the elements in item.array.
If any match is found in it, the subquery will return, and the result is
then post-filtered for matches (think of HAVING in SQL).

Best regards
Jan
Reply all
Reply to author
Forward
0 new messages