Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
Let us look at some examples of the syntax we have learned so far using MATCH
and RETURN
keywords.
Each example will start with an explanation of what we are trying to achieve and have an image below of the results of the query run in Neo4j Browser.
Example 1: Find the labeled Person
nodes in the graph.
Note that we must use a variable like p
for the Person
node if we want retrieve the node in the RETURN
clause.
MATCH (p:Person)
RETURN p
Example 2: Find Person
nodes in the graph that have a name of 'Jennifer'.
Remember that we can name our variable anything we want, as long as we reference that same name later.
MATCH (jenn:Person {name: 'Jennifer'})
RETURN jenn
Example 3: Find which Company
Jennifer works for.
Explanation: we know we need to find Jennifer’s Person
node, and we need to find the Company
node she is connected to.
To do that, we need to follow the WORKS_FOR
relationship from Jennifer’s Person
node to the Company
node.
We have also specified a label of Company
so that the query will only look at nodes with that label.
Since we only care about returning the company in this query, we need to give that node a variable (company
) but do not need to give variables for the Person
node or WORKS_FOR
relationship.
MATCH (:Person {name: 'Jennifer'})-[:WORKS_FOR]->(company:Company)
RETURN company
Example 4: Find which Company
Jennifer works for, but this time, return only the name of the company.
Explanation: this query is very similar to Example 3.
Example 3 returned the entire Company
node with all its properties.
For this example, we still need to find Jennifer’s company, but now we only care about its name.
We will need to access the node’s name
property using the syntax variable.property
to return the name value.
MATCH (:Person {name: 'Jennifer'})-[:WORKS_FOR]->(company:Company)
RETURN company.name