Querying documents and graphs in one database with AQL – easily

General Tags: , 1 Comment

Note: The following article was originally written as an answer in ArangoDB google group. It may help other people to understand the scope of ArangoDB and/or AQL, so we posted it here as well.

AQL, the query language, provides access to the data which is stored inside collections. The collections contain documents, identified by unique keys.

For the following examples, I’ll create a “users” collection with a few documents inside:

You can now access the data in this collection via AQL queries like this:
Return all users the collection, with all attributes:

Return a specific user (“fred”) from the collection, identified by its unique id:

Return a few documents from the collection, identified by some non-key attributes (returns users stu and john):

Create some projection:

As you can see, you can use AQL for various kinds of data accesses (all documents, by primary key, by non-key-attributes etc.). Using an AQL query for simple data access patterns works fine but is a bit of an overkill. There are also specialised methods for simple data access patterns. But this should only be relevant if you plan on issuing a lot of queries.

In addition to the simple access patterns, AQL also supports rather complex queries. You can do many things with it that you can do with SQL, though AQL is more specialised for non-relational data.

We haven’t covered graphs yet. Graphs in ArangoDB can be established by connecting documents via so-called “edge” documents. The connected documents are then called “vertices” as in graph theory. Node that vertices and edges are documents, too. That means they can have any attributes you like. You can query these documents either as part of a graph using AQL or dedicated other functionality, but also individually (not as part of a graph).

To illustrate this, we’ll use the documents in the “users” collection as the vertices, and create an extra collection named “knows” for the edges. We have used the “users” data already (and we still can) but we can now use the documents in the “users” collection as part of a graph. Here’s an example setup for the connections between the users.

We can now use this mini-graph to run some AQL queries on it:
Query for “who is known (directly) by Carla” returns:

Query connections from Carla to others, via other users known by Carla

Query “Who knows Carla returns: ana”

In ArangoDB 1.1, PATHS() is the only graph-related functionality available in AQL. There is more you can do with graphs in ArangoDB. We hope to release ArangoDB version 1.2 soon, and it will provide a lot more graph-related functionality in AQL.

Tweet about this on TwitterShare on Google+Share on RedditShare on LinkedInBuffer this page

About Jan Steemann

Jan is an expert in data modeling with NoSQL & relational databases and writing high performance web applications. For ArangoDB, he wrote most of AQL (ArangoDB’s query language). He also blogs at https://jsteemann.github.io
  • jsteemann

    Actually I didn’t write this as an article…
    Seems someone else posted a digest of what I wrote on the ArangoDB Google group here.