Graph Queries

Perform native graph traversals and pattern matching directly within SoliDB using simple SDBQL extensions.

Overview

SoliDB treats every document relation as a graph edge. No special setup is required—just use standard collections and documents to model nodes and edges. CREATE_GRAPH(name, {vertices, edges}) stores a catalog document in _graphs; GRAPH name then uses the first edge collection. A bare edge-collection name still works. In addition to traversals, built-in graph algorithms such as PAGERANK and DEGREE_CENTRALITY are available via SDBQL. K_SHORTEST_PATHS uses Yen’s algorithm.

Vertices

Regular documents in any collection. Examples: Users, Products, Locations.

Edges

Documents that connect two vertices via `_from` and `_to` fields.

Collection Types

SoliDB supports two types of collections for graph modeling:

  • Document Standard collections used to store vertices (nodes). Contains your data objects.
  • Edge Special collections used to store edges (relationships). These documents MUST contain `_from` and `_to` fields.

Creating Edge Collections

POST /_api/database/_system/collection
{
  "name": "friendships",
  "type": 2  // 2 for Edge Collection (default is 1 for Document)
}

Or use the "Create" button in the Collections dashboard and select "Edge Collection".

Edge Documents

Edge documents are normal JSON documents but require two system fields:

{
  "_from": "users/alice",
  "_to": "users/bob",
  "type": "friend_request",
  "created_at": 123456789
}

SDBQL Graph Queries

Use the `TRAVERSE` keyword to walk the graph.

TRAVERSE OUTBOUND | INBOUND | ANY
FROM start_vertex
VIA edge_collection
DEPTH 1..3

Parameters

Direction
`OUTBOUND` (follows edges pointing away), `INBOUND` (points to), or `ANY` (both).
start_vertex
The `_id` of the document to start traversing from (e.g., `users/123`).
edge_collection
The collection containing the edge relationships.

Common Patterns

Find Friends of Friends

Social Network
FOR user IN users
  FILTER user._key == 'alice'
  FOR friend IN OUTBOUND user 
  GRAPH 'friendships'
  OPTIONS { bfs: true, uniqueVertices: 'global' }
  RETURN friend.name

This query starts at Alice, follows outgoing edges in the `friendships` graph for depth 1, and returns names of found vertices.

Traversal, PRUNE, and shortest path

Routing
FOR v, e, p IN 1..3 OUTBOUND start follows
  PRUNE v.blocked == true
  RETURN p

FOR p IN OUTBOUND SHORTEST_PATH
  'cities/paris' TO 'cities/berlin'
  GRAPH 'roads'
  RETURN p.name

Third identifier p is the walk (vertices / edges). PRUNE stops expansion. The second query finds a hop-count shortest path.