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.
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.
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.
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.
There are actually two options:
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With