0

I would like to check if a query (filter) is not empty, something like this in PostgreSQL:

select EXISTS(select 1 from tab where tab.name = 'abc')

https://www.postgresql.org/docs/11/functions-subquery.html

My current approach in AQL is:

RETURN LENGTH(for d in tab filter d.name = 'abc' RETURN 1) > 0
Robert Zaremba
  • 8,081
  • 7
  • 47
  • 78

1 Answers1

2

The sub-query FOR d IN tab FILTER d.name == 'abc' may find a matching document early on, but will continue looking for more because there is no limit defined. Therefore, I would recommend:

RETURN LENGTH(FOR d IN tab FILTER d.name == 'abc' LIMIT 1 RETURN true) > 0

This would return [ true ] as query result.

You could also RETURN d starting from v3.4.0. The optimizer rule optimize-subqueries recognizes the pattern LENGTH(... RETURN doc) and replaces doc with a constant value true.

CodeManX
  • 11,159
  • 5
  • 49
  • 70