How to count number of elements with AQL?
Asked Answered
C

2

7

I need to count elements that was in result of SQL query: db._query('FOR v in visitors FILTER v.ip == "127.0.0.1" return COUNT(v.guid) ')

This request is return my a length of every GUID, but I need to get total number of GUIDs for example: 2.

Convey answered 6/5, 2016 at 16:15 Comment(1)
Did the answer fullfill your needs? If not, whats missing? If, can you mark it accepted?Crutchfield
C
10

You need to use the result of the query as input for the COUNT function, and then RETURN this result.

You can replace the RETURN value of the actual query by 1 for performance reasons:

RETURN COUNT(FOR v IN visitors FILTER v.ip == "127.0.0.1" RETURN 1)
Crutchfield answered 9/5, 2016 at 9:44 Comment(4)
This didn't work for me, I had to use LENGTH rather than COUNT.Romansh
COUNT is actually an alias of LENGTH, so you're invoking the same code path inside of the ArangoDB server. Whatever you changed in your query must have been more than swapping the both to make it work.Crutchfield
Mhh. Nope. Just changing that one word. If I use COUNT I get [ArangoError 1540: usage of unknown function 'COUNT()' (while parsing)]. Maybe the alias was introduced in a version newer than the one I use.Romansh
The currently supported versions don't show that behaviour. Maybe you should upgrade? Which version are you running? Maybe a github issue would be better to continue this.Crutchfield
G
3

Version from 2022!

FOR m IN messages 
FILTER DATE_HOUR(m.date) == 3
COLLECT WITH COUNT INTO length
RETURN length
Granddaughter answered 13/5, 2022 at 19:10 Comment(1)
Great, at last they have this functionality. :)Staciestack

© 2022 - 2024 — McMap. All rights reserved.