Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArangoDB link documents

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.

like image 803
GBrian Avatar asked Oct 27 '14 14:10

GBrian


People also ask

What is ArangoDB document?

Documents in ArangoDB are JSON objects. These objects can be nested (to any depth) and may contain lists. Each document has a unique primary key which identifies it within its collection. Furthermore, each document is uniquely identified by its document handle across all collections in the same database.

How does ArangoDB store data?

At the very bottom of the ArangoDB database system lies the storage engine. The storage engine is responsible for persisting the documents on disk, holding copies in memory, providing indexes and caches to speed up queries.

Is ArangoDB a relational database?

Both approaches have their pros and cons. ArangoDB is somewhat in-between: You can both model and query your data in a “relational way” but also in an “aggregate-oriented way”, depending on your use case. ArangoDB offers joins, nesting of sub-documents and multi-collection graphs.


1 Answers

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.

like image 170
mchacki Avatar answered Sep 28 '22 01:09

mchacki