{ "subject" : "ArangoDB", "tags": [ "multi-model", "nosql", "database" ] }

AQL Object Literal Simplification

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:

example query
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:

the long notation for object literals
RETURN { age: age, length: length } 

In this case, the new shorthand notation simplifies the RETURN to:

using shorthand notation for object literals
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.:

mixing long and shorthand notation
RETURN { age, length, dateCreated: DATE_NOW() }