Skip to main content

🔍 MATCH: Pattern Matching

MATCH (n) RETURN n
MATCH (n:Person {name: 'Alice'}) RETURN n.age
MATCH (a)-[e]->(b) RETURN *
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)-[NEIGHBORS_OF]->(susan),
...
  • Labels required for node creation
  • No RETURN clause
  • Use commas to create multiple items at once

🎯 Property Match Operators

MATCH (n {name: 'Alice'}) RETURN n
MATCH (n {name ~= 'apoe'}) RETURN n        -- Approximate match
MATCH (n {age: 42u}) RETURN n              -- Unsigned int
  • : → exact match
  • ~= → string approximation (prefix-based, case-insensitive)

📦 Data Types

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

✨ RETURN Clause

MATCH (n)--(m) RETURN n.name, n.age, m.name, m.age
MATCH (n)--(m) RETURN *
  • RETURN * → expands to all variables
  • Select fields with RETURN n.prop, m.prop

📌 Node ID Injection

MATCH (n @ 1) RETURN n
MATCH (n:Person @ 1, 2, 3) RETURN n.name
  • Use @ or AT to reference internal IDs

🔬 Metaqueries (CALL)

QueryDescription
CALL db.properties()All node/edge property keys & types
CALL db.labels()All node labels
CALL db.edgetypes()All edge types
CALL db.labelsets()Combinations of labels

🔧 Engine Commands

CommandDescription
CREATE GRAPH <name>Create a new named graph
LOAD GRAPH <name>Load an existing graph
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

-- All nodes with approx match
MATCH (n {name ~= 'kinase'}) RETURN n

📘 Best Practices

  • Use CALL db.properties() to discover property keys
  • Use RETURN * while prototyping
  • Use @ for precise node editing or merging
  • Avoid regex — use ~= for faster fuzzy search
  • All edges are directed