Skip to content

Commit 1fe76db

Browse files
Node and relationship operators (neo4j#1241)
1 parent c29a726 commit 1fe76db

File tree

5 files changed

+189
-187
lines changed

5 files changed

+189
-187
lines changed

modules/ROOT/content-nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
*** xref:expressions/predicates/string-operators.adoc[]
7070
*** xref:expressions/predicates/path-pattern-expressions.adoc[]
7171
*** xref:expressions/predicates/type-predicate-expressions.adoc[]
72+
** xref:expressions/node-relationship-operators.adoc[]
7273
** xref:expressions/mathematical-operators.adoc[]
7374
** xref:expressions/string-operators.adoc[]
7475
** xref:expressions/temporal-operators.adoc[]
Lines changed: 1 addition & 0 deletions
Loading

modules/ROOT/pages/expressions/index.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
= Expressions
2+
:description: Information about the expressions available in Cypher.
3+
:page-aliases: syntax/operators.adoc
4+
25

36
A Cypher expression is any part of a query that evaluates to a value.
47
For details and examples of specific expressions, see the following sections:
@@ -11,10 +14,10 @@ For details and examples of specific expressions, see the following sections:
1114
** xref:expressions/predicates/string-operators.adoc[]: `STARTS WITH`, `ENDS WITH`, `CONTAINS`, `IS NORMALIZED`, `IS NOT NORMALIZED`, `=~`
1215
** xref:expressions/predicates/path-pattern-expressions.adoc[]: information about filtering queries with path pattern expressions.
1316
** xref:expressions/predicates/type-predicate-expressions.adoc[]: information about how to verify the value type of a Cypher expression.
17+
* xref:expressions/node-relationship-operators.adoc[]: information about how to access `NODE` and `RELATIONSHIP` property values with `.` and `[]`.
1418
* xref:expressions/mathematical-operators.adoc[]: `+`, `-`, `*`, `/`, `%`, `^`.
1519
* xref:expressions/string-operators.adoc[]: `+`, `||`
1620
* xref:expressions/temporal-operators.adoc[]: `+`, `-`, `*`, `/`
1721
* xref:expressions/list-expressions.adoc[]: information about list concatenation operators (`||`, `+`), list element access, list slicing, and list as well as pattern comprehensions.
1822
* xref:expressions/map-expressions.adoc[]: information about map operators (`.`, `[]`) and map projection.
1923
* xref:expressions/conditional-expressions.adoc[]
20-
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
= Node and relationship operators
2+
:description: Information about Cypher's node and relationship operators, which enable the querying and manipulation of nodes and relationships.
3+
4+
Node and relationship operators allow you to manipulate and query `NODE` and `RELATIONSHIP` property values.
5+
Cypher contains the following node and relationship operators:
6+
7+
* Static property access: dot operator (`.`)
8+
* Dynamic property access: subscript operator (`[]`)
9+
10+
For functions that return metadata about `NODE` and `RELATIONSHIP` values, see:
11+
12+
* xref:functions/scalar.adoc#functions-elementid[`elementId()`]
13+
* xref:functions/scalar.adoc#functions-endnode[`endNode()`]
14+
* xref:functions/scalar.adoc#functions-id[`id()`]
15+
* xref:functions/list.adoc#functions-keys[`keys()`]
16+
* xref:functions/list.adoc#functions-labels[`labels()`]
17+
* xref:functions/scalar.adoc#functions-properties[`properties()`]
18+
* xref:functions/scalar.adoc#functions-startnode[`startNode()`]
19+
* xref:functions/scalar.adoc#functions-type[`type()`]
20+
21+
22+
[[example-graph]]
23+
== Example graph
24+
25+
The following graph is used for the examples below:
26+
27+
image::graph_element_operators.svg[width="600",role="middle"]
28+
29+
To recreate the graph, run the following query against an empty Neo4j database:
30+
31+
[source, cypher, role=test-setup]
32+
----
33+
CREATE (alice:Person {firstName:'Alice', middleName: 'Catherine', lastName: 'Baxter'}),
34+
(cecil:Person {firstName: 'Cecil', middleName: 'David', lastName: 'Ericson'}),
35+
(cecilia:Person {firstName: 'Cecilia', lastName: 'Farega'}),
36+
(cecil)-[:WORKS_FOR {since: 2023}]->(alice),
37+
(cecilia)-[:WORKS_FOR {since: 2015}]->(alice)
38+
----
39+
40+
[[static-property-access]]
41+
== Static property access
42+
43+
Property values can be accessed statically by specifying a property name after the `.` operator.
44+
45+
.Access node properties statically
46+
[source, cypher]
47+
----
48+
MATCH (p:Person)
49+
RETURN p.firstName AS name
50+
----
51+
52+
.Result
53+
[role="queryresult",options="header,footer",cols="1*<m"]
54+
|===
55+
| name
56+
57+
| "Alice"
58+
| "Cecil"
59+
| "Cecilia"
60+
61+
1+d|Rows: 3
62+
|===
63+
64+
.Access node and relationship properties statically
65+
[source, cypher]
66+
----
67+
MATCH (employee:Person)-[r:WORKS_FOR]-(manager:Person)
68+
RETURN employee.firstName AS employee,
69+
r.since AS employedSince,
70+
manager.firstName AS manager
71+
----
72+
73+
.Result
74+
[role="queryresult",options="header,footer",cols="3*<m"]
75+
|===
76+
| employee | employedSince | manager
77+
78+
| "Cecil" | 2023 | "Alice"
79+
| "Cecilia" | 2015 | "Alice"
80+
81+
3+d|Rows: 2
82+
|===
83+
84+
[[dynamic-property-access]]
85+
== Dynamic property access
86+
87+
Property values can be accessed dynamically by using the subscript operator, `[]`.
88+
89+
.Access properties dynamically using a variable
90+
[source, cypher]
91+
----
92+
LET nodeProperty = 'lastName'
93+
MATCH (p:Person)
94+
RETURN p[nodeProperty] AS lastName
95+
----
96+
97+
.Result
98+
[role="queryresult",options="header,footer",cols="1*<m"]
99+
|===
100+
| lastName
101+
102+
| "Baxter"
103+
| "Ericson"
104+
| "Farega"
105+
106+
1+d|Rows: 3
107+
|===
108+
109+
110+
.Parameters
111+
[source, parameters]
112+
----
113+
{
114+
"propertyName": 'middleName'
115+
}
116+
----
117+
118+
.Access properties dynamically using a parameter
119+
[source, cypher]
120+
----
121+
MATCH (:Person)
122+
RETURN p[$propertyName] AS middleName
123+
----
124+
125+
.Result
126+
[role="queryresult",options="header,footer",cols="1*<m"]
127+
|===
128+
| middleName
129+
130+
| "Catherine"
131+
| "David"
132+
| NULL
133+
134+
1+d|Rows: 3
135+
|===
136+
137+
== Handling `NULL` values
138+
139+
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`.
140+
The query below performs a xref:expressions/string-operators.adoc[string concatentation] on the `firstName`, `middleName`, and `lastName` properties on `Person` nodes.
141+
Note that `NULL` is returned for `Cecilia`, who lacks a `middleName` property.
142+
143+
.String concatenation using node properties
144+
[source, cypher]
145+
----
146+
MATCH (p:Person)
147+
RETURN p.firstName || ' ' || p.middleName || ' ' || p.lastName AS fullName
148+
----
149+
150+
.Result
151+
[role="queryresult",options="header,footer",cols="1*<m"]
152+
|===
153+
| fullName
154+
155+
| "Alice Catherine Baxter"
156+
| "Cecil David Ericson"
157+
| NULL
158+
159+
1+d|Rows: 3
160+
|===
161+
162+
The xref:functions/scalar.adoc#functions-coalesce[`coalesce()`] function can be used to skip the first `NULL` values in an expression.
163+
In the below example, it replaces the first `NULL` value found with an empty `STRING`.
164+
165+
.Use the `coalesce()` function to skip `NULL` values
166+
[source, cypher]
167+
----
168+
MATCH (p:Person)
169+
RETURN p.firstName || coalesce(' ' + p.middleName, '') || ' ' || p.lastName AS fullName
170+
----
171+
172+
173+
.Result
174+
[role="queryresult",options="header,footer",cols="1*<m"]
175+
|===
176+
| fullName
177+
178+
| "Alice Catherine Baxter"
179+
| "Cecil David Ericson"
180+
| "Cecilia Farega"
181+
182+
1+d|Rows: 3
183+
|===

0 commit comments

Comments
 (0)