The most powerful way to query your data in ArangoDB is by using ArangoDB’s own query language, AQL. In the past using AQL in your JavaScript code sadly would often require writing long, unwieldy strings. This made writing complex queries difficult and could often lead to subtle syntax errors or mistakes.

The ArangoDB Query Builder (AQB) is a JavaScript node packaged module that provides a fluid API for writing AQL queries in plain JavaScript. And if you’re using ArangoDB 2.3, the aqb module is already available to your Foxx applications. Let’s say we want to perform the following query in our Foxx application using the new query builder:

First we need to import the query builder module:

The query builder provides chainable methods for the AQL keywords:

Note that the keyword methods are lowercase to fit in more naturally in JavaScript. Also note that reference names and literals don’t need any special escaping. If you pass in a string, the query builder will assume it’s meant to be a reference name and throw an error if it is not well-formed:

In order to define filters and other expressions, the query builder provides helper methods:

Let’s say we want to build the query dynamically and use a variable instead of simply passing in true. The query builder provides additional methods to enforce data types, for example:

You can even construct reference names dynamically:

In Foxx applications you generally want to use auto-prefixed collection names to avoid conflicts between multiple copies of the same application. Wherever the query builder expects a reference name for a collection, you can simply pass in a collection object directly:

In ArangoDB 2.3 and newer, you can also simply pass in query builder instances wherever an AQL query string would normally be expected:

If you want to use the query builder with older versions of ArangoDB or outside of ArangoDB entirely, you can just call its toAQL method to convert it into a regular string:

Here’s the full translation of the AQL we started with:

Now let’s use what we’ve learned to turn that query into something more flexible:

Note that you can also use the query builder to construct parameterized queries:

More information on the ArangoDB Query Builder can be found on GitHub and on NPM.

If you want to continue with other JavaScript related resources, you should start with ArangoDB NoSQL and JavaScript.