Skip to main content

Vertices and Edges

This page discusses best practices for determining what data should be stored in edges and vertices in Macrometa graphs.

Vertices

Vertices are the main objects in your graph data model, such as users, groups, or articles. For each type of object, a document collection (also called a vertex collection) stores the individual entities. Entities can be connected by edges to express and classify relations between vertices. It often makes sense to have one edge collection per relation type.

Macrometa does not require you to store your data in graph structures with edges and vertices. You can also decide to embed attributes, such as which groups a user is part of or _ids of documents in another document, instead of connecting the documents with edges. This can be a meaningful performance optimization for 1:n relationships, if your data is not focused on relations and you don't need graph traversal with varying depth. However, it usually means introducing some redundancy and possibly inconsistencies if you embed data, so it should be considered as a tradeoff.

For example, let's say you have two vertex collections: Users and Groups. Documents in the Groups collection contain the attributes of the Group, such as when it was founded, its subject, an icon URL, and so on. Users documents contain the data specific to a user, such as all names, birthdays, avatar URLs, or hobbies.

Edges

An edge collection is used to store relations between users and groups. Since multiple users might be in an arbitrary number of groups, this is an m:n relation.

For example, the edge collection can be called UsersInGroups, with one edge having _from pointing to Users/John and _to pointing to Groups/BowlingGroupHappyPin. This makes the user John a member of the group Bowling Group Happy Pin. Attributes of this relation might contain qualifiers to this relation, such as the permissions of John in this group or the date when he joined the group.

Edges could also be between connections between customers and products, where each edge is either a customer ordering a product or the customer liking or disliking a product.

If you use documents and their attributes in a sentence, nouns would typically be vertices, and verbs the edges.

Edges have a direction, with their relations _from and _to pointing from one document to another document stored in vertex collections. In queries you can define in which directions the edge relations may be followed i.e.,

  • OUTBOUND: _from_to
  • INBOUND: _from_to
  • ANY: _from_to

Advantages of this Approach

Graphs give you the advantage of not just being able to have a fixed number of m:n relations in a row, but an arbitrary number. Edges can be traversed in both directions (outbound and inbound), making it easy to determine all groups a user is in, but also to find out which members a certain group has. Users could also be interconnected to create a social network.

Using the graph data model, dealing with data that has lots of relations stays manageable and can be queried in very flexible ways, whereas it would be difficult to handle in a relational database system.