Skip to content

Instantly share code, notes, and snippets.

@chrisma
Last active October 1, 2019 16:18
Show Gist options
  • Save chrisma/480ba000d37443a6f728 to your computer and use it in GitHub Desktop.
Save chrisma/480ba000d37443a6f728 to your computer and use it in GitHub Desktop.
Neo4J cheatsheet

Neo4J Cheatsheet

Refcard

http://neo4j.com/docs/2.1/cypher-refcard/

Database Exploration

List all labels of a node

labels(n)

List all node labels (and count them)

MATCH n
RETURN distinct labels(n), count(n) as count_n
ORDER BY count_n desc

List all relationships (and count them)

http://neo4j.com/docs/milestone/query-functions-scalar.html#functions-type

MATCH ()-[r]-()
RETURN distinct type(r), count(r) as count_r
ORDER BY count_r desc

List all nodes that don't have any relationships

MATCH (n)-[r]-()
WHERE r is null
RETURN n, count(n)

What is related, and how

MATCH (a)<-[r]-(b)
RETURN DISTINCT labels(a) AS This, type(r) as To, labels(b) AS That

Useful queries

Match by property value

MATCH (n:User {id:1234})
RETURN n

or

MATCH (n:User)
WHERE n.id = 1234
RETURN n

Find nodes that do NOT have a certain relationship

MATCH (u:User)
WHERE NOT (u)-[:likes]->(:Seafood)
RETURN u.name as UserName

Match nodes using case-insensitive regexp

MATCH (u:User)
WHERE u.name =~ '(?i)Alex.*'
RETURN u.name

Nodes that don't have two relationships (of a specific type)

MATCH (u:User)
WHERE NOT (:User)<-[:likes]-(u)-[:likes]->(:User)
RETURN u.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment