|
| 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