Arangodb LET variable from AQL for use in FILTER

I have a collection of "Notifier". Given the following example of this collection:

{ "timestamp": 1413543986, "message": "message", "readed": { "user_8": 0, "user_9": 0, "user_22": 0 }, "type": "1014574149174" } 

I am trying to find this document with the following AQL:

 FOR n IN Notifier LET usersFound = TO_BOOL( FOR user IN n.readed FILTER user == 'user_8' LIMIT 1 RETURN user ) FILTER usersFound==true RETURN n 

I get the following error:

[1501] syntax error, unexpected FOR declaration, pending) next to OR user IN n.readed FILT ... 'at position 3:10

How can I write this AQL correctly with LET?

+5
source share
2 answers

Your request is almost correct, there are 2 minor problems.

1) TO_BOOL () are the characters for starting the correction function, now you want to insert a subquery that starts by transferring the AQL instruction to the function (). Therefore, instead of TO_BOOL (AQL) you should use: TO_BOOL ((AQL));

2) n.readed - a JSON object, FOR x IN expects a list. When you repeat the n.readed attributes, you can use ATTRIBUTES (n.readed) here.

Here is the right solution for your example:

 FOR n IN Notifier LET usersFound = TO_BOOL( (FOR user IN ATTRIBUTES(n.readed) FILTER user == 'user_8' LIMIT 1 RETURN user) ) FILTER usersFound==true RETURN n 

PS: If you are only looking for an attribute and do not want to do further filtering, you can get this a little easier using HAS:

 FOR n IN Notifier LET usersFound = HAS(n.readed, 'user_8') FILTER usersFound==true RETURN n 
+5
source

If you are just looking for the presence of a field whose value can be converted to a boolean, you do not need to worry about LET and subqueries. The following query uses the HAS function to determine if the specified field is inside the document and BOOL to convert its value to a logical representation:

 LET search = 'user_8' FOR n IN Notifier FILTER HAS(n.readed, search) == true && TO_BOOL(n.readed[search]) RETURN n 
+2
source

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


All Articles