简体   繁体   English

如何检查ArangoDB查询是否为空?

[英]How to check if ArangoDB query is not empty?

I would like to make an exists PostgreSQL query. 我想做一个exists PostgreSQL查询。

Let's say I have a Q ArangoDB query (AQL). 假设我有一个Q ArangoDB查询(AQL)。 How can I check if Q returns any result? 如何检查Q是否返回任何结果?

Example: 例:

Q = "For u in users FILTER 'x@example.com' = u.email"

What is the best way to do it (most performant)? 最好的方法是什么(表现最好)?

I have ideas, but couldn't find an easy way to measure the performance: 我有想法,但是找不到衡量性能的简单方法:

Idea 1: using Length : 想法1:使用Length

RETURN LENGTH(%Q RETURN 1) > 0

Idea 2: using Frist : 想法2:使用Frist

RETURN First(%Q RETURN 1) != null

Above, %Q is a substitution for the query defined at the beginning. 以上, %Q替代了开头定义的查询。

Do you need and AQL solution? 您需要AQL解决方案吗?

Only the count: 仅计数:

var q  = "For u in users FILTER 'x@example.com' = u.email";
var res = db._createStatement({query: q, count: true}).execute();
var ct = res.count();

Is the fastest I can think of. 是我能想到的最快的。

I think the best way to achieve this for a generic selection query with a structure like 我认为对于具有以下结构的通用选择查询,实现此目标的最佳方法

Q = "For u in users FILTER 'x@example.com' = u.email" Q =“对于用户中的FILTER'x@example.com'= u.email”

is to first add a LIMIT clause to the query, and only make it return a constant value (in contrast to the full document). 首先向查询添加LIMIT子句,然后仅使其返回常量值(与完整文档相反)。

For example, the following query returns a single match if there is such document or an empty array if there is no match: 例如,以下查询返回单个匹配(如果存在此类文档),或者返回空数组(如果不存在匹配项):

FOR u IN users FILTER 'x@example.com' == u.email LIMIT 1 RETURN 1

(please note that I also changed the operator from = to == because otherwise the query won't parse). (请注意,我还将运算符从=更改为==因为否则查询将不会解析)。

Please note that this query may benefit a lot from creating an index on the search attribute, ie email . 请注意,通过在搜索属性上创建索引(例如email ,该查询可能会受益匪浅。 Without the index the query will do a full collection scan and stop at the first match, whereas with the index it will just read at most a single index entry. 没有索引,查询将进行完整的集合扫描,并在第一个匹配项时停止,而有了索引,它将最多读取单个索引条目。

Finally, to answer your question, the template for the EXISTS -like query will then become 最后,为回答您的问题, EXISTS的查询的模板将变为

LENGTH(%Q LIMIT 1 RETURN 1)

or fleshed out via the example query: 或通过示例查询充实:

LENGTH(FOR u IN users FILTER 'x@example.com' == u.email LIMIT 1 RETURN 1)

LENGTH(...) will return the number of matches, which in this case will either be 0 or 1. And it can also be used in filter conditions like as follows LENGTH(...)将返回匹配数,在这种情况下将为0或1。它还可用于如下所示的过滤条件

FOR ....
  FILTER LENGTH(...)
  RETURN ...

because LENGTH(...) will be either 0 or 1, which in context of a FILTER condition will evaluate to either false or true. 因为LENGTH(...)将为0或1,这在FILTER条件的情况下将为false或true。

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

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