12
votes

Is it possible to link documents from different collections in ArangoDB as it is in OrientDB?

In OrientDB you can create a field of type LINK and specify the type linked. That creates a relation between both documents.

Do I have to use edge collections to do this in ArangoDB?

I'm trying to define a main collection and a secondary collection with extra information to supplement the main one. I don't want to have all the data in the main collection as this is shared between other entities.

Thanks in advance.

1

1 Answers

16
votes

There are actually two options:

  1. Using Joins

    You can define an attribute on the main document containing information that identifies the sub-document (e.g. by its _key) and then use AQL to join the two documents in your query:

    FOR x IN maindocuments FILTER x.whatever < 42 FOR y in secondarydocuments FILTER x.sub = y._key RETURN MERGE(x,y)

  2. Using Edges

    You can define an edge collection holding all the "relations" between your documents. The edge documents can also optionally additional information on the edges themselves.

    FOR x in maindocuments LET n = NEIGHBORS("maindocuments", "edgecollection", x._id, "any"); RETURN MERGE(x, n[0].vertex);

However there is no such thing like a foreign key constraint in ArangoDB. You can refer to non-existing documents in your edges or delete the sub-document without the main document being informed.

The benefit of the second approach is that you can use an arbitrary number of edges between these documents and even decide on 0, 1 or more during runtime of your application without any modification. With the first approach you have to decide that at the beginning by making the attribute a single value or a list of values.