Prev |  | Next

## 3.2. Relationships

Relationships between nodes are a key part of a graph database. They allow for finding related data. Just like nodes, relationships can have properties.

A relationship connects two nodes, and is guaranteed to have valid start and end nodes.

As relationships are always directed, they can be viewed as outgoing or incoming relative to a node, which is useful when traversing the graph:

Relationships are equally well traversed in either direction. This means that there is no need to add duplicate relationships in the opposite direction (with regard to traversal or performance).

While relationships always have a direction, you can ignore the direction where it is not useful in your application.

Note that a node can have relationships to itself as well:

To further enhance graph traversal all relationships have a relationship type. Note that the word type might be misleading here, you could rather think of it as a label. The following example shows a simple social network with two relationship types.

Using relationship direction and type

What | How
--- | ---
get who a person follows | outgoing `follows` relationships, depth one
get the followers of a person | incoming `follows` relationships, depth one
get who a person blocks | outgoing `blocks` relationships, depth one
get who a person is blocked by | incoming `blocks` relationships, depth one

This example is a simple model of a file system, which includes symbolic links:

Depending on what you are looking for, you will use the direction and type of relationships during traversal.

What | How
--- | ---
get the full path of a file | incoming `file` relationships
get all paths for a file | incoming `file` and `symbolic link` relationships
get all files in a directory | outgoing `file` and `symbolic link` relationships, depth one
get all files in a directory, excluding symbolic links | outgoing `file` relationships, depth one
get all files in a directory, recursively | outgoing `file` and `symbolic link` relationships

Copyright © 2013 Neo Technology

Prev | Up | Next
| Home |