Nodes: Represent entities. Can have properties (key-value pairs) and labels (typed groups).
Browse / Neo4j Cypher Cheatsheet
Neo4j Cypher Cheatsheet
A quick reference guide to Neo4j basics, data modeling concepts, and essential Cypher query language syntax and commands.
Neo4j Basics & Concepts
Core Concepts
|
Relationships: Connect nodes. Always directed, have a type, and can have properties. |
Properties: Key-value pairs stored on nodes or relationships. Values can be primitives (string, number, boolean) or arrays of primitives. |
Labels: Typed groups for nodes. A node can have multiple labels. Used for indexing and constraints. |
Relationship Types: Typed connections between nodes. A relationship must have exactly one type. |
Schema-Free: Neo4j is schema-flexible. Properties and labels/types are not strictly defined at creation but are typically enforced with constraints. |
Graph Structure: Data is stored as connected nodes and relationships, optimizing for traversals. |
Cypher: Neo4j’s declarative query language for working with the graph. |
Node Syntax
() |
An anonymous node. |
(n) |
A node bound to variable |
(:Label) |
A node with a specific label. |
(n:Label) |
A node bound to variable |
(n:Label:Label2) |
A node with multiple labels. |
({key: 'value'}) |
A node with properties. |
(:Label {key: 'value', num: 123}) |
A node with a label and properties. |
(n:Label {key: 'value'}) |
A node bound to |
Relationship Syntax
--> |
A directed relationship. |
<-- |
A directed relationship (reverse direction). |
-- |
An undirected relationship (not common in queries, primarily for representation). |
-[:TYPE]-> |
A relationship with a specific type. |
-[r:TYPE]-> |
A relationship bound to variable |
-[:TYPE|TYPE2]-> |
A relationship with one of multiple types. |
-[:TYPE {key: 'value'}]-> |
A relationship with a type and properties. |
-[:TYPE*]-> |
Variable length relationship (1 or more). |
-[:TYPE*2..5]-> |
Variable length relationship (2 to 5 hops). |
Graph Patterns
|
|
|
|
|
|
Multiple Patterns:
|
Optional Match:
Returns persons even if they don’t have a |
Essential Cypher Queries
Create Data
Create a node:
|
Create multiple nodes:
|
Create a relationship:
|
Create a node and relationship simultaneously:
|
Create nodes with multiple labels:
|
Create a relationship with multiple properties:
|
Create a variable-length relationship (rarely used in CREATE, more in MATCH):
|
Match Data
Match all nodes:
|
Match nodes with a specific label:
|
Match a node by property:
|
Match a node by property using WHERE:
|
Match a specific relationship type:
|
Match relationships with specific properties:
|
Match paths:
|
Match nodes connected by any relationship type:
|
Merge Data
Merge a node (create if not exists, find if exists):
|
Merge a relationship (create if pattern not exists):
|
ON CREATE / ON MATCH clauses:
|
ON CREATE / ON MATCH clauses:
|
Combined ON CREATE and ON MATCH:
|
Merge complex patterns:
|
Using MERGE on relationship with properties:
|
Merging multiple paths:
|
Update & Delete Data
SET - Set or update properties/labels. Set a property:
|
REMOVE - Remove properties/labels. Remove a property:
|
Set multiple properties:
|
Remove multiple properties:
|
Set a label:
|
Remove a label:
|
Update relationship properties:
|
Remove relationship properties:
|
DELETE - Delete nodes and relationships. Delete relationships (nodes remain):
|
DETACH DELETE - Delete nodes and their relationships. Delete a node and its relationships:
|
Delete nodes based on a condition:
|
Delete all nodes and relationships:
Caution: This empties the database! |
Return & Ordering
RETURN - Specify what to output. Return nodes and relationships:
|
ORDER BY - Sort results. Order by node property (ascending):
|
Return specific properties:
Use |
Order by node property (descending):
|
Return distinct results:
|
Order by multiple properties:
|
Return aggregated values (COUNT, SUM, AVG, MIN, MAX, COLLECT, etc.):
|
Ordering on aggregates:
|
SKIP & LIMIT - Pagination. Skip first N results:
|
Limit results to N:
|
Combine SKIP and LIMIT:
|
Return paths:
|
Predicates & Functions
Comparison:
|
Boolean:
|
Regular Expressions:
(Case-insensitive starts with ‘alice’) |
String Predicates:
|
List Predicates:
|
Null Check:
|
Existence Check:
|
Relationship type:
|
Node labels:
|
Path length:
|
Functions: Get properties:
|
Get keys:
Get ID:
|
Aggregating Functions: Count all nodes:
|
Collect properties into a list:
|
Constraints & Indexes
Constraints enforce rules on data, like uniqueness. |
Create a uniqueness constraint (also creates an index):
|
Create a node property existence constraint:
|
Create a relationship property existence constraint:
|
Create a composite uniqueness constraint:
|
Create a B-tree index on a node property:
|
Create a B-tree index on a relationship property:
|
Drop a constraint:
|
Drop an index:
|
Show constraints:
|
Show indexes:
|