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, []
.
LET nodeProperty = 'lastName'
MATCH (p:Person)
RETURN p[nodeProperty] AS lastName
lastName |
---|
|
|
|
Rows: 3 |
{
"propertyName": 'middleName'
}
MATCH (: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 |