RETURN

The RETURN statement can be used to produce the result of a query. It is mandatory to specify a RETURN statement at the end of each block in a data-selection query, otherwise the query will be invalid. Using RETURN on the main level in data-modification queries is optional.

The general syntax for RETURN is:

RETURN expression

The expression returned by RETURN is produced for each iteration in the block the RETURN statement is placed in. That means the result of a RETURN statement is always an array when inside FOR loop.

To return all elements from the currently iterated array without modification, the following simple form can be used:

FOR variableName IN expression RETURN variableName

As RETURN allows specifying an expression, arbitrary computations can be performed to calculate the result elements. Any of the variables valid in the scope the RETURN is placed in can be used for the computations.

To iterate over all elements of an array of objects and return the full objects, you can write:

FOR i IN [{name: 'Mike', age: 30}, {name: 'James', age: 35}] RETURN i

In each iteration of the for-loop, an element of the array is assigned to a variable i and returned unmodified in this example. To return only one attribute of each element, you could use a different return expression:

FOR i IN [{name: 'Mike', age: 30}, {name: 'James', age: 35}] RETURN i.name

Or to return multiple attributes, an object can be constructed like this:

FOR i IN [{firstName: 'Mike', lastName: 'Wazowski', age: 30}, {firstName: 'James', lastName: 'Sullivan', age: 35}] RETURN { name: i.firstName + ' ' + i.lastName, age: i.age }

Dynamic attribute names are supported as well:

FOR i IN [{name: 'Mike', age: 30}, {name: 'James', age: 35}] RETURN { [i.name]: i.age }