Goals This guide explains the basic concepts of Cypher, Neo4j’s query language. You should be able to read and understand Cypher queries after finishing this guide. Prerequisites You should have familiarized yourself with Graph Databases and the Property Graph Model.… Learn More »
This guide explains the basic concepts of Cypher, Neo4j’s query language. You should be able to read and understand Cypher queries after finishing this guide.
You should have familiarized yourself with Graph Databases and the Property Graph Model.
Cypher is a declarative, SQL-inspired language for describing patterns in graphs. It allows us to describe what we want to select, insert, update or delete from a graph database without requiring us to describe exactly how to do it.
Cypher uses ASCII-Art to represent patterns. We surround nodes with parentheses which look like circles, e.g.
(node). If we later want to refer to the node, we’ll give it an identifier like
(p) for person or
(t) for thing. In real-world queries, we’ll probably use longer, more expressive variable names like
To illustrate, if we want to find all the persons and the things they like, the query will include the identifiers person and thing. We might use a pattern like
(person)-->(thing) so we can refer to them later, for example, to access properties like
The more general structure is:
The problem with the Cypher snippets we saw above is that they didn’t say anything about the relationship between the nodes, so even though we used the identifier person, we may well have gotten back suppliers and sellers of our things. So we need to be able to describe the types of relationships in our Cypher queries.
If we wanted to retrieve everyone who likes a thing, we would describe the pattern
(person)-[:LIKE]->(thing) to retrieve only nodes that had a relationship typed LIKE with other nodes (thing). Those nodes would then be persons as implied by the LIKE relationship.
Sometimes we need access to information about a relationship (e.g. its type or properties). For example, we might want to output the rating that a person gave a thing and that rating would probably be a property of the LIKE relationship. As with nodes, we can use identifiers for relationships (in front of the :TYPE). If we tried to match
(person)-[how:LIKE]->(thing), we would be able to output the
how.rating for each of the persons and all the things that they like.
Labels allow us to assign roles or types to our nodes. For example, we might want to distinguish things from persons or companies (both deal with things). By matching for
(person:Person)-[:LIKE]->(thing), it will return John, but not ACME Inc–a well known manufacturer.
Labels are usually used like this:
Let’s try out what we’ve learned.
Create a Record for Yourself
Create creates nodes with labels and properties.
You like the NoSQL, Graph Database Neo4j, right?
Create can create single nodes, or more complex structures in one go.
Create Your Friends
Foreach allows you to execute operations for each element of a list.