Comparing ArangoDB AQL to Neo4j Cypher

Comparing ArangoDB AQL to Neo4j Cypher

ArangoDB is a multi-model database, and one of its supported data models are graphs. If you come from Neo4j, this comparison should help you to get started with ArangoDB’s graph related features, but also demonstrate what else it can do for you using the other data models and features.

webinar arangodb
Neo4j Logo

New to ArangoDB? Take Free ArangoDB Graph course for freshers.

Language Models

Cypher is a graph query language, created by and primarily used in Neo4j. The pattern you want to find in the full graph is described in a visual way, like ASCII art. Around that, clauses inspired by SQL like WHERE, ORDER BY and others are used to process the data. It also covers data definition with the CREATE keyword. The language can be classified as declarative, but less structured than SQL. There are also functions which can be called, like shortestPath().

AQL is ArangoDB’s own query language. It was invented to overcome the limitations of SQL for dealing with schemaless data and the JSON document model. It enables multi-model queries with one language backed by a single database core. What that means is that you can do, for example, a prefix search over multiple collections and fields (ArangoSearch), then a traversal from the found documents to neighbor nodes at a variable depth, then resolve values in the found documents by using a join, and all that in a single query at high speed. AQL is declarative, but also borrows concepts from programming languages. A lot of core functionality is based around the FOR loop construct. There are also plenty of functions. CRUD operations are supported via the INSERT, UPDATE, REPLACE and REMOVE constructs, but collections and indices can’t be created or managed through AQL. It can be done in the server’s web interface, arangosh (an interactive shell we ship) or through the HTTP API instead.

Graph Database Concepts in ArangoDB

Naming convention comparison

Here is a quick overview of terms which describe similar concepts:

AQL Cypher
vertex node
edge relationship
collection (group of nodes)
document (node with properties)
document collection node label
edge collection relationship type
attribute property
depth hops
array list
object map

While you can use arbitrary labels and types in Neo4j in an ad-hoc fashion, it is necessary to create collections in ArangoDB before you can insert vertices and edges into them. Collections can be created via different interfaces, but not with the query language AQL. You may create secondary indices on collections for faster lookup speeds. Collections can be organized in databases for multi-tenancy. There is a default database _system which you can use to get started.

ArangoDB Architecture Employee manages

Keyword Comparison

The basic language constructs and their keywords in comparison:

AQL Cypher
FOR … IN … RETURN MATCH … RETURN
FOR … IN UNWIND
FILTER WHERE
SORT ORDER BY
LIMIT count LIMIT count
LIMIT offset, count SKIP offset LIMIT count
OUTBOUND* -->
INBOUND* <--
ANY --
INSERT … INTO CREATE
UPDATE … IN SET
REPLACE … IN SET
REMOVE … IN DELETE

* in Cypher you express the edge direction as stored or you use two hyphens to traverse an edge either way. To follow edges in the opposite direction only, you would not flip the arrow but change the query to return the other end of a path. In AQL, you provide a start vertex and control the traversal direction with a keyword: OUTBOUND to follow in edge direction, INBOUND to follow in reverse direction only or ANY to traverse the edge regardless of the direction.

Example Data

We use a simple company graph for our comparison:

Employee Graph

As you can see, edges point from superior to subordinate in our demonstration. Beside their names, we will also give them a job title (role) and an age:

Name Role Age
Ann Boss 42
Tracey Developer Lead 35
Josefina Marketing Lead 29
Sammy Programmer 35
Eryn Frontend Developer 51
Quinn Graphics Designer 42
Mark Marketing Operations 35

To store this data in ArangoDB, we use a trivial model:

  • Our employee nodes will be stored in a document collection Employee
  • The relations will be stored in an edge collection manages

Data Model in ArangoDB

A few remarks:

  • Collections need to be created before data can be inserted into them. You can use the ArangoDB’s web interface to do so.
  • Every collection has a primary index on a special property, the _key attribute. This index is automatically created and can not be removed. The _key attribute stores the document key as string, which is unique within a collection.
  • There is a virtual attribute _id for stored documents, which is the concatenation of the collection name, a forward slash and the document key. It uniquely identifies a document within a database.
  • Edges are also documents in ArangoDB, but with special _from and _to attributes which reference other documents (nodes). Because documents are JSON objects you may store arbitrary attributes on edges, including nested objects.
  • Edge collections have a special edge index built-in, which enables fast graph traversals. It indexes the _from and _to attributes, which reference other documents using _id values.
Graph Structure

To try out the AQL queries presented below, get ArangoDB if you don’t have it already, then open its web interface, go to COLLECTIONS and create a document collection Employee and an edge collection manages. Then click on QUERIES and run the following query:

This will create the regular documents and the edge documents in the two collections.

Basic Traversals

The basic syntax for traversals in AQL is as follows:

Graph Traversal Syntax explained 2

Let us compare some queries so that you understand how it works.

Get the employees directly managed by Ann:

Result of AQL query:

Find the superior of Tracey:

Result of AQL query:

Get the employees managed by Ann, directly and indirectly (up to two levels, which means the entire graph in our example):

Result of AQL query:

Traversals in AQL default to a depth of 1, so FOR … IN OUTBOUND … means the minimum and maximum number of hops will be 1. If you write FOR … IN 2 … then the minimum as well as the maximum will be 2. To specify different values you write it as shown in above query. Traversals with an unlimited depth like in Cypher using an asterisk (*) is not supported in AQL, but you may set a very high maximum.

Pattern Matching

In ArangoDB, we call traversals with conditions pattern matching. Without conditions it would be a simple traversal, even though in Cypher every search may be considered a pattern matching.

Using the previous query, let us extend it with filter conditions. In below example, we want to find employees at least 30 and at most 35, managed by Ann directly or indirectly:

Result of AQL query:

Shortest Path

We can determine the official channel for Quinn to pass a message on to Eryn by finding the shortest path between them. We follow in any direction, because the edge orientation changes midway at Ann. If you know that the direction doesn’t change on the paths you are interested, then use either directed traversals, so INBOUND or OUTBOUND in AQL.

Employee Shortest path

Result of AQL query:

Aggregation

AQL comes with a broad aggregation framework to group by one or multiple values. It can also be used to calculate things like an average value on the fly. The following example could also use a graph traversal, but for simplicity we just use all employee records we have to calculate the average age, rounded to a full number:

Result of AQL query:

Here is a simple example how to group by age and count how many employees are of the same age:

Result of AQL query:

Discover more

Not everything matches the graph model and has to be traversal. You can also do joins in AQL to replace a traversal with a depth of 1. A join operation can be more efficient for one-hop relations. See our AQL tutorial for an example.

Want to learn more about graphs in ArangoDB? Get our free Graph Course!

Miss something in our comparison? Leave us a comment or write to learn@arangodb.com

Do you like ArangoDB?
icon-githubStar this project on GitHub.
close-link