ArangoDB v3.13 is under development and not released yet. This documentation is not final and potentially incomplete.
LET
operation in AQL
You can use the LET
operation to assign an arbitrary value to a variable
The variable is introduced in the scope the LET
statement is placed in.
You cannot change the value once assigned.
Syntax
LET variableName = expression
expression can be a simple expression or a subquery.
For allowed variable names AQL Syntax.
Usage
Variables are immutable in AQL, which means they cannot be re-assigned:
LET a = [1, 2, 3] // initial assignment
a = PUSH(a, 4) // syntax error, unexpected identifier
LET a = PUSH(a, 4) // parsing error, variable 'a' is assigned multiple times
LET b = PUSH(a, 4) // allowed, result: [1, 2, 3, 4]
LET
statements are mostly used to declare complex computations and to avoid
repeated computations of the same value at multiple parts of a query.
FOR u IN users
LET numRecommendations = LENGTH(u.recommendations)
RETURN {
"user" : u,
"numRecommendations" : numRecommendations,
"isPowerUser" : numRecommendations >= 10
}
In the above example, the computation of the number of recommendations is
factored out using a LET
statement, thus avoiding computing the value twice in
the RETURN
statement.
Another use case for LET
is to declare a complex computation in a subquery,
making the whole query more readable.
FOR u IN users
LET friends = (
FOR f IN friends
FILTER u.id == f.userId
RETURN f
)
LET memberships = (
FOR m IN memberships
FILTER u.id == m.userId
RETURN m
)
RETURN {
"user" : u,
"friends" : friends,
"numFriends" : LENGTH(friends),
"memberShips" : memberships
}