Thursday, October 29, 2020

Neo4j - Selecting data with MATCH using Cypher

MATCH (p:Person)

WHERE p.Name = "Devin Townsend"

RETURN p


The WHERE clause works the same way as SQL's WHERE clause, in that it allows you to narrow down the results by providing extra criteria.


However, you can achieve the same result without using a WHERE clause. You can also search for a node by providing the same notation you used to create the node.


The following code provides the same results as the above statement:



MATCH (p:Person {Name: "Devin Townsend"})

RETURN p



Relationships


You can also traverse relationships with the MATCH statement. In fact, this is one of the things Neo4j is really good at.


For example, if we wanted to find out which artist released the album called Heavy as a Really Heavy Thing, we could use the following query:



MATCH (a:Artist)-[:RELEASED]->(b:Album)

WHERE b.Name = "Heavy as a Really Heavy Thing" 

RETURN a



ou can see that the pattern we use in the MATCH statement is almost self-explanatory. It matches all artists that released an album that had a name of Heavy as a Really Heavy Thing.


We use variables (i.e. a and b) so that we can refer to them later in the query. We didn't provide any variables for the relationship, as we didn't need to refer to the relationship later in the query.


You might also notice that the first line uses the same pattern that we used to create the relationship in the first place. This highlights the simplicity of the Cypher language. We can use the same patterns in different contexts (i.e. to create data and to retrieve data).



Return all Nodes


You can return all nodes in the database simply by omitting any filtering details. Therefore, the following query will return all nodes in the database:



MATCH (n) RETURN n



Limit the Results


Use LIMIT to limit the number of records in the output. It's a good idea to use this when you're not sure how big the result set is going to be.


So we could simply append LIMIT 5 to the previous statement to limit the output to 5 records:




References:

https://www.quackit.com/neo4j/tutorial/neo4j_select_data_with_match_using_cypher.cfm


No comments:

Post a Comment