Examples

Every output below was produced by GraphBuff itself, on the movies graph from examples/movies.cypher or the small data each example creates. Timings are left out.

Who acted with whom

Cypher

A two-hop pattern with an aggregate and a collected list.

MATCH (k:Person {name: 'Keanu Reeves'})-[:ACTED_IN]->(m)<-[:ACTED_IN]-(co)
RETURN co.name AS coActor, count(m) AS movies, collect(m.title) AS titles
ORDER BY movies DESC, coActor;
+--------------------+--------+---------------------------------------+
| coActor            | movies | titles                                |
+--------------------+--------+---------------------------------------+
| "Carrie-Anne Moss" | 2      | ["The Matrix", "The Matrix Reloaded"] |
| "Hugo Weaving"     | 1      | ["The Matrix"]                        |
+--------------------+--------+---------------------------------------+
2 rows

Shortest path between two people

Cypher

A bidirectional breadth-first search, then a list comprehension over the path.

MATCH p = shortestPath((h:Person {name: 'Hugo Weaving'})-[*]-(l:Person {name: 'Lana Wachowski'}))
RETURN [n IN nodes(p) | coalesce(n.name, n.title)] AS path;
+--------------------------------------------------+
| path                                             |
+--------------------------------------------------+
| ["Hugo Weaving", "The Matrix", "Lana Wachowski"] |
+--------------------------------------------------+
1 row

Everyone within four hops

Cypher

Variable-length patterns in any direction, deduplicated.

MATCH (h:Person {name: 'Hugo Weaving'})-[*1..4]-(other:Person)
WHERE other <> h
RETURN DISTINCT other.name AS reachable ORDER BY reachable;
+--------------------+
| reachable          |
+--------------------+
| "Carrie-Anne Moss" |
| "Keanu Reeves"     |
| "Lana Wachowski"   |
+--------------------+
3 rows

Upsert with a uniqueness constraint

Cypher

MERGE creates the node once; the constraint keeps names unique; temporal values are first-class.

CREATE CONSTRAINT person_name IF NOT EXISTS FOR (p:Person) REQUIRE p.name IS UNIQUE;

MERGE (p:Person {name: 'Laurence Fishburne'})
  ON CREATE SET p.born = 1961, p.created = date('2026-11-13')
  ON MATCH SET p.seen = true
RETURN p.name, p.born, p.created;
1 indexes added, 1 constraints added
+----------------------+--------+------------+
| p.name               | p.born | p.created  |
+----------------------+--------+------------+
| "Laurence Fishburne" | 1961   | 2026-11-13 |
+----------------------+--------+------------+
1 row; 1 nodes created, 3 properties set, 1 labels added

Counting subqueries and durations

Cypher

COUNT { } inline, and date arithmetic with duration.between.

MATCH (m:Movie)
RETURN m.title AS title,
       COUNT { (m)<-[:ACTED_IN]-() } AS cast,
       duration.between(date({year: m.released}), date('2026-01-01')).years AS yearsAgo
ORDER BY m.released;
+-----------------------+------+----------+
| title                 | cast | yearsAgo |
+-----------------------+------+----------+
| "The Matrix"          | 3    | 27       |
| "The Matrix Reloaded" | 2    | 23       |
| "John Wick"           | 1    | 12       |
+-----------------------+------+----------+
3 rows

Distance between two places

Cypher

WGS-84 points and haversine distance in metres.

WITH point({longitude: 14.42, latitude: 50.09}) AS prague,
     point({longitude: 16.61, latitude: 49.20}) AS brno
RETURN round(point.distance(prague, brno) / 1000) AS km;
+-------+
| km    |
+-------+
| 186.0 |
+-------+
1 row