简体   繁体   English

来自AQL的Arangodb LET变量在FILTER中使用它

[英]Arangodb LET variable from AQL for use it in FILTER

I have collection 'Notifier'. 我有'Notifier'集合。 Given the following example document of this collection: 给出此集合的以下示例文档:

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

I try find this document with the following AQL: 我尝试使用以下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, expecting ) near 'OR user IN n.readed FILT...' at position 3:10 [1501]语法错误,意外的FOR声明,期待)在位置3:10的'OR user IN n.readed FILT ...'附近

How can I write this AQL correctly using LET? 如何使用LET正确编写此AQL?

Your query is almost correct, there are 2 minor issues. 您的查询几乎是正确的,有2个小问题。

1) TO_BOOL() are chars to trigger the correspoding function, you now want to insert a subquery, that is triggered by wrapping an AQL statement in additional (). 1)TO_BOOL()是用于触发correspoding函数的字符,您现在想要插入一个子查询,该子查询是通过在Additional()中包装AQL语句来触发的。 So instead of TO_BOOL(AQL) you have to use: TO_BOOL((AQL)); 因此,不必使用TO_BOOL(AQL):TO_BOOL((AQL));

2) n.readed is a JSON object, FOR x IN expectes a list. 2)n.readed是一个JSON对象,FOR x IN是一个列表。 As you are iterating over the Attributes of n.readed you can use ATTRIBUTES(n.readed) here. 在迭代n.readed的Attributes时,可以在这里使用ATTRIBUTES(n.readed)。

Here is the correct 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 the exisence of an attribute and do not want to do further filtering you can get that a bit easier using HAS: PS:如果您只是寻找属性的外观并且不想进行进一步过滤,那么使用HAS可以更容易一些:

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

If you are just looking for a presence of some field which value can be converted to boolean value you don't need to bother with LET and subqueries. 如果您只是想要某个字段的存在哪个值可以转换为布尔值,则无需使用LET和子查询。 Following query uses HAS function to determine presence of specified field inside document and BOOL to convert it's value to boolean representation: 以下查询使用HAS函数来确定文档和BOOL中指定字段的存在,以将其值转换为布尔表示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM