AQL Object Literal Simplification: ArangoDB Query Optimization
ArangoDB’s devel
branch recently saw a change that makes writing some AQL queries a bit simpler.
The change introduces an optional shorthand notation for object attributes in the style of ES6’s enhanced object literal notation.
For example, consider the following query that groups values by age
attribute and counts the number of documents per distinct age
value:
FOR doc IN collection
COLLECT age = doc.age WITH COUNT INTO length
RETURN { age: age, length: length }
The object declaration in the last line of the query is somewhat redundant because one has to type identical attribute names and values:
RETURN { age: age, length: length }
In this case, the new shorthand notation simplifies the RETURN
to:
RETURN { age, length }
In general, the shorthand notation can be used for all object literals when there is an attribute name that refers to a query variable of the same name.
It can also be mixed with the longer notation, e.g.:
RETURN { age, length, dateCreated: DATE_NOW() }
Get the latest tutorials, blog posts and news: