Node and relationship operators
Node and relationship operators allow you to manipulate and query NODE and RELATIONSHIP property values.
Cypher® contains the following node and relationship operators:
-
Static property access: dot operator (
.) -
Dynamic property access: subscript operator (
[])
For functions that return metadata about NODE and RELATIONSHIP values, see:
Example graph
The following graph is used for the examples below:
To recreate the graph, run the following query against an empty Neo4j database:
CREATE (alice:Person {firstName:'Alice', middleName: 'Catherine', lastName: 'Baxter'}),
(cecil:Person {firstName: 'Cecil', middleName: 'David', lastName: 'Ericson'}),
(cecilia:Person {firstName: 'Cecilia', lastName: 'Farega'}),
(cecil)-[:WORKS_FOR {since: 2023}]->(alice),
(cecilia)-[:WORKS_FOR {since: 2015}]->(alice)
Static property access
Property values can be accessed statically by specifying a property name after the . operator.
MATCH (p:Person)
RETURN p.firstName AS name
| name |
|---|
|
|
|
Rows: 3 |
MATCH (employee:Person)-[r:WORKS_FOR]->(manager:Person)
RETURN employee.firstName AS employee,
r.since AS employedSince,
manager.firstName AS manager
| employee | employedSince | manager |
|---|---|---|
|
|
|
|
|
|
Rows: 2 |
||
Dynamic property access
Property values can be accessed dynamically by using the subscript operator, [].
WITH 'lastName' AS nodeProperty
MATCH (p:Person)
RETURN p[nodeProperty] AS lastName
| lastName |
|---|
|
|
|
Rows: 3 |
{
"propertyName": "middleName"
}
MATCH (p:Person)
RETURN p[$propertyName] AS middleName
| middleName |
|---|
|
|
|
Rows: 3 |
Handling null values
If a property (or property value) is missing in an expression that uses tries to access a property statically or dynamically, the whole expression will evaluate to null.
The query below performs a string concatentation on the firstName, middleName, and lastName properties on Person nodes.
Note that null is returned for Cecilia, who lacks a middleName property.
MATCH (p:Person)
RETURN p.firstName || ' ' || p.middleName || ' ' || p.lastName AS fullName
| fullName |
|---|
|
|
|
Rows: 3 |
The coalesce() function can be used to skip the first null values in an expression.
In the below example, it replaces the first null value found with an empty STRING.
coalesce() function to skip null valuesMATCH (p:Person)
RETURN p.firstName || coalesce(' ' + p.middleName, '') || ' ' || p.lastName AS fullName
| fullName |
|---|
|
|
|
Rows: 3 |