Graph Queries

Native graph traversals and shortest path algorithms for exploring connected data.

Graph Traversal Operations

SoliDB supports native graph traversals and shortest path algorithms using dedicated keywords.

TRAVERSAL Graph Traversal

Traverse the graph starting from a vertex.

FOR vertex[, edge] IN [min..max] DIRECTION startVertex edgeCollection
OUTBOUND
_from → _to
INBOUND
_to ← _from
ANY
Either direction

Direct (1 Hop)

FOR v IN OUTBOUND "users/alice" follows
  RETURN v.name

Variable Depth (1..2 Hops)

FOR v, e IN 1..2 ANY "users/alice" follows
  RETURN { user: v.name, type: e.type }

SHORTEST_PATH Shortest Path

Find the shortest path between two vertices.

FOR vertex[, edge] IN SHORTEST_PATH start TO end DIRECTION edgeCollection

Find Path

FOR v, e IN OUTBOUND SHORTEST_PATH
  "users/alice" TO "users/charlie"
  follows
  RETURN { vertex: v.name, edge: e }

Practical Examples

Find All Friends of Friends

FOR friend IN 2..2 OUTBOUND "users/alice" follows
  RETURN DISTINCT friend.name

Find Mutual Followers

LET aliceFollows = (
  FOR v IN OUTBOUND "users/alice" follows
    RETURN v._key
)
LET bobFollows = (
  FOR v IN OUTBOUND "users/bob" follows
    RETURN v._key
)
RETURN INTERSECTION(aliceFollows, bobFollows)

Traverse with Edge Filtering

FOR v, e IN 1..3 OUTBOUND "users/alice" follows
  FILTER e.weight > 0.5
  RETURN { user: v.name, relationship: e.type }