Use AQL Variables eg. to count (LET sum = sum + 1)
According to https://www.arangodb.com/2014/07/13/arangodb-2-2-0-released it should be possible to use expressions like this:
LET sum = 0
FOR v IN values
SORT v.year
LET sum = sum + v.value
RETURN { year: v.year, value: v.value, sum: sum }
I am currently using version 2.4 but cannot use it eg. in a statement like this:
LET sum = 0
FOR i in memoryColl
LET sum = sum + 1
// sum = sum + 1
RETURN { "i": i, "sum": sum }
I got error [1511] variable 'sum' is assigned multiple times (while parsing)
Can someone tell me if such a statemtn should work in principle, and how exactly?
source to share
As explained in the docs update for version 2.3, it is no longer possible to update variables in queries:
Previous versions of ArangoDB allowed modification of variables inside AQL queries [...]
While this is admittedly a handy feature, the design of the new query optimizer doesn't allow it .
Also, updating variables within a query would have prevented many of the optimizations to queries that we would like the optimizer to do. Additionally, updating variables in queries that run on different nodes in the cluster is not deterministic because queries are not linearly executed.
To list the docs, you could do
LET range = 0..LENGTH(memoryColl)-1
FOR i IN range
RETURN {i: i+1, doc: memoryColl[i]}
but it looks really bad to me. Better return the documents and let the client list them.
If you really want to count the number of documents, you can use a subquery:
LET result = (
FOR doc IN memoryColl
FILTER True // add some condition here for instance
RETURN doc
)
RETURN LENGTH(result)
2.4 can also be considered more efficient:
http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/
source to share