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:

graph element operators

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.

Access node properties statically
MATCH (p:Person)
RETURN p.firstName AS name
Table 1. Result
name

"Alice"

"Cecil"

"Cecilia"

Rows: 3

Access node and relationship properties statically
MATCH (employee:Person)-[r:WORKS_FOR]-(manager:Person)
RETURN employee.firstName AS employee,
       r.since AS employedSince,
       manager.firstName AS manager
Table 2. Result
employee employedSince manager

"Cecil"

2023

"Alice"

"Cecilia"

2015

"Alice"

Rows: 2

Dynamic property access

Property values can be accessed dynamically by using the subscript operator, [].

Access properties dynamically using a variable
LET nodeProperty = 'lastName'
MATCH (p:Person)
RETURN p[nodeProperty] AS lastName
Table 3. Result
lastName

"Baxter"

"Ericson"

"Farega"

Rows: 3

Parameters
{
  "propertyName": 'middleName'
}
Access properties dynamically using a parameter
MATCH (:Person)
RETURN p[$propertyName] AS middleName
Table 4. Result
middleName

"Catherine"

"David"

NULL

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.

String concatenation using node properties
MATCH (p:Person)
RETURN p.firstName || ' ' || p.middleName || ' ' || p.lastName AS fullName
Table 5. Result
fullName

"Alice Catherine Baxter"

"Cecil David Ericson"

NULL

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.

Use the coalesce() function to skip NULL values
MATCH (p:Person)
RETURN p.firstName || coalesce(' ' + p.middleName, '') || ' ' || p.lastName AS fullName
Table 6. Result
fullName

"Alice Catherine Baxter"

"Cecil David Ericson"

"Cecilia Farega"

Rows: 3