Skip to main content

MATCH: Pattern Matching

MATCH (n) RETURN n
MATCH (n:Person {name: 'Alice'}) RETURN n.age
MATCH (a:Person)-->(b:Company) RETURN a.name, b.name
  • () → Node
  • [] → Edge
  • - → Undirected edge
  • Labels: :Label
  • Properties: {key: 'value', age: 30}

CREATE: Add Nodes & Edges

CREATE (:Person {name: 'Alice', age: 30})
CREATE (:Person {name: 'Mick'})-[:FRIEND_OF]->(:Person {name: 'John'})
CREATE (a)-[:EDGE]->(b), (b)-[:EDGE]->(c), (c)-[:EDGE]->(a)

CREATE (gabby:Person {name: 'Gabby', age: 27}),
(susan:Person {name: 'Susan', age: 35}),
(lynette:Person {name: 'Lynette', age: 41}),
(bree:Person {name: 'Bree', age: 42}),
(gabby)-[FRIEND_OF]->(susan),
(gabby)-[FRIEND_OF]->(lynette),
...
  • Labels required for node creation
  • No RETURN clause
  • Use commas to create multiple items at once

MATCH + CREATE and MATCH + CREATE + RETURN

// Create two nodes Person
CREATE (:Person {name: 'Alice', age: 24}),
	   (:Person {name: 'John', age: 27})

// Match two nodes to create the edge between them
MATCH (n:Person {name: 'Alice'}), (m:Person {name: 'John'})
CREATE (n)-[:FRIENDS_WITH]->(m)

// RETURN clause can also be added
MATCH (n:Person {name: 'Alice'}), (m:Person {name: 'John'})
CREATE (n)-[:FRIENDS_WITH]->(m)
RETURN n.name, n.age, m.name, m.age

WHERE: Filter nodes and edges by properties

To filter on node label:
MATCH (n)
WHERE n:Person
RETURN n, n.age
To filter on node property:
MATCH (n)
WHERE n.name = 'Alice'
RETURN n, n.age
To filter on edge label:
MATCH (n)-[e]->(m)
WHERE e:PLAY_POKER_TOGETHER
RETURN n.name, m.age
To filter on edge property:
MATCH (n)-[e]->(m:Person)
WHERE n.name = 'Gabby'
AND e.play_poker_together = true
RETURN n.name, m.age

Property Match Operators

MATCH (n {name: 'Alice'}) RETURN n
// exact equivalent of
MATCH (n) WHERE n.name = 'Alice' RETURN n
  • : → exact match using property filter in node directly
  • = → exact match using property filter in WHERE clause

Boolean operators

  • OR
    MATCH (n:Person)
    WHERE n.medication = "Aspirin"
    OR n.medication = "Ibuprofen"
    RETURN n.name
    
  • AND
    MATCH (n)
    WHERE n.name = 'Matt'
    AND n.age = 20
    AND n.hasPhD = false
    RETURN n
    

Comparison operators

  • Equal: =
    # Find antibodies targeting proteins in Human
    MATCH (ab:Antibody)-->(prot:Protein)
    WHERE prot.host = 'Human'
    RETURN ab.name, prot.name
    
  • Inequal: <>
    # Find antibodies (associated to a protein) used together
    # in same publication (2-hop)
    MATCH (ab1:Antibody)-->(prot:Protein), (ab2:Antibody)-->(prot:Protein)
    WHERE ab1.name <> ab2.name
    RETURN ab1.name, ab2.name, prot.name, prot.gene_name
    
  • Less than: <
  • Less than or equal to: <=
  • Greater than: >
  • Greater than or equal to: >=
    # Publications published on 2020 or after
    MATCH (pub:Publication)
    WHERE pub.published_year >= 2020
    RETURN pub.displayName, pub.pubmedid, pub.published_year, pub.country
    
  • is null:IS NULL
  • is not null:IS NOT NULL
    # Publications from the United States
    MATCH (pub:Publication)
    WHERE pub.country = 'United States'
    AND pub.institution IS NOT NULL
    RETURN pub.displayName, pub.institution, pub.published_year
    

Data Types

TypeExample
String'hello' or "hello"
Integerage=30
Booleanflag=true
Doublescore=3.14

RETURN Clause

MATCH (n)-->(m) RETURN n.name, n.age, m.name, m.age
  • Select fields with RETURN n.prop, m.prop

LOAD graphs

Load TuringDB graph

Files have to be available in graphs subdirectory of turing-dir.
LOAD graph_name

Load external data into TuringDB graph

Files have to be available in data subdirectory of turing-dir.
LOAD GML 'mygml.gml' AS my_graph
LOAD JSONL 'myjsonl.jsonl' AS my_graph

Metaqueries (CALL)

QueryDescription
CALL db.propertyTypes()All node/edge property keys & types
CALL db.labels()All node labels
CALL db.edgeTypes()All edge types
CALL db.history()Commit history

Engine Commands

CommandDescription
CREATE GRAPH <name>Create a new named graph
LOAD GRAPH <name>Load an existing graph
LOAD GML 'mygraph.gml' AS my_graphLoad the specified GML as TuringDB graph. Requires the GML to be accessible in TuringDB directory (--turing-dir, data subdir)
LOAD JSONL 'mygraph.gml' AS my_graphLoad the specified JSONL as TuringDB graph. Requires the JSONL to be accessible in TuringDB directory (--turing-dir, data subdir)
LIST GRAPHList all available graphs
HISTORYShow commit history (versioning)
CHANGE NEWCreate a new isolated change
CHANGE SUBMITMerge current change into main
CHANGE DELETEDelete current change
CHANGE LISTList active uncommitted changes

Useful Patterns

// Two-hop connection
MATCH (a)-[r1]->(b)-[r2]->(c) RETURN c

// All people named John
MATCH (n:Person {name: 'John'}) RETURN n
/// exact equivalent of
MATCH (n:Person) WHERE n.name = 'John' RETURN n