Skip to content

Commit 002170d

Browse files
Cypher 25 cheat sheet tags (neo4j#1247)
Co-authored-by: Richard Sill <[email protected]>
1 parent c04464b commit 002170d

21 files changed

+247
-31
lines changed

modules/ROOT/pages/clauses/create.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ This is because a relationship can only have exactly one type.
234234
----
235235

236236
.Create nodes and relationships using dynamic node labels and relationship types
237+
// tag::clauses_create_dynamic_create[]
237238
[source, cypher]
238239
----
239240
CREATE (greta:$($nodeLabels) {name: 'Greta Gerwig'})
@@ -242,6 +243,7 @@ UNWIND $movies AS movieTitle
242243
CREATE (greta)-[rel:$($relType)]->(m:Movie {title: movieTitle})
243244
RETURN greta.name AS name, labels(greta) AS labels, type(rel) AS relType, collect(m.title) AS movies
244245
----
246+
// end::clauses_create_dynamic_create[]
245247

246248
.Result
247249
[role="queryresult",options="footer",cols="4*<m"]

modules/ROOT/pages/clauses/finish.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
[[query-finish]]
33
= FINISH
44

5-
A query ending in `FINISH` instead of `RETURN` has no result but executes all its side effects.
5+
A query ending in `FINISH` -- instead of `RETURN` -- has no result but executes all its side effects.
66
`FINISH` was introduced as part of Cypher's xref:appendix/gql-conformance/index.adoc[].
77

88
The following read query successfully executes but has no results:
99

1010
.Query
11+
// tag::clauses_finish_match[]
1112
[source, cypher]
1213
----
1314
MATCH (p:Person)
1415
FINISH
1516
----
17+
// end::clauses_finish_match[]
1618

1719
The following query has no result but creates one node with the label `Person`:
1820

modules/ROOT/pages/clauses/foreach.adoc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ CREATE
3333
[[foreach-mark-all-nodes-along-a-path]]
3434
== Mark all nodes along a path
3535

36-
This query will set the property `marked` to `true` on all nodes along a path.
36+
This query sets the property `marked` to `true` on all nodes along a path.
3737

3838
.Query
39+
// tag::clauses_foreach_node[]
3940
[source, cypher, indent=0]
4041
----
4142
MATCH p=(start)-[*]->(finish)
4243
WHERE start.name = 'A' AND finish.name = 'D'
4344
FOREACH (n IN nodes(p) | SET n.marked = true)
4445
----
46+
// end::clauses_foreach_node[]
4547

4648
.Result
4749
[role="queryresult",options="footer",cols="1*<m"]
@@ -51,3 +53,47 @@ d|Rows: 0 +
5153
Properties set: 4
5254
|===
5355

56+
57+
[[foreach-mark-all-relationships-along-a-path]]
58+
== Mark all relationships along a path
59+
60+
This query sets the property `marked` to `true` on all relationships along a path.
61+
62+
// tag::clauses_foreach_relationship[]
63+
[source, cypher, indent=0]
64+
----
65+
MATCH p=(start)-[*]->(finish)
66+
WHERE start.name = 'A' AND finish.name = 'D'
67+
FOREACH ( r IN relationships(p) | SET r.marked = true )
68+
----
69+
// end::clauses_foreach_relationship[]
70+
71+
.Result
72+
[role="queryresult",options="footer",cols="1*<m"]
73+
|===
74+
|(empty result)
75+
d|Rows: 0 +
76+
Properties set: 3
77+
|===
78+
79+
[[foreach-create-new-nodes-form-a-list]]
80+
== Create new nodes from a list of name labels
81+
82+
This query creates a new node for each label in a list.
83+
84+
.Query
85+
// tag::clauses_foreach_create[]
86+
[source, cypher, indent=0]
87+
----
88+
WITH ['E', 'F', 'G'] AS names
89+
FOREACH ( value IN names | CREATE (:Person {name: value}) )
90+
----
91+
// end::clauses_foreach_create[]
92+
93+
.Result
94+
[role="queryresult",options="footer",cols="1*<m"]
95+
|===
96+
1+|(empty result)
97+
1+d|Rows: 0 +
98+
Nodes created: 3
99+
|===

modules/ROOT/pages/clauses/limit.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ Properties set: 1
166166
`LIMIT` can be used as a standalone clause, or in conjunction with xref:clauses/order-by.adoc[`ORDER BY`] or xref:clauses/skip.adoc[`SKIP`]/xref:clauses/skip.adoc#offset-synonym[`OFFSET`].
167167

168168
.Standalone use of `LIMIT`
169+
// tag::clauses_limit_standalone[]
169170
[source, cypher]
170171
----
171172
MATCH (n)
172173
LIMIT 2
173174
RETURN collect(n.name) AS names
174175
----
176+
// end::clauses_limit_standalone[]
175177

176178
.Result
177179
[role="queryresult",options="header,footer",cols="1*<m"]
@@ -185,6 +187,7 @@ The following query orders all nodes by `name` descending, skips the two first r
185187
It then xref:functions/aggregating.adoc#functions-collect[collects] the results in a list.
186188

187189
.`LIMIT` used in conjunction with `ORDER BY` and `SKIP`
190+
// tag::clauses_limit[]
188191
[source, cypher]
189192
----
190193
MATCH (n)
@@ -193,6 +196,7 @@ SKIP 2
193196
LIMIT 2
194197
RETURN collect(n.name) AS names
195198
----
199+
// end::clauses_limit[]
196200

197201
.Result
198202
[role="queryresult",options="header,footer",cols="1*<m"]

modules/ROOT/pages/clauses/load-csv.adoc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ By default, paths are resolved relative to the Neo4j import directory.
4646
----
4747
4848
.Query
49+
// tag::clauses_load_csv_local_files[]
4950
[source, cypher]
5051
----
5152
LOAD CSV FROM 'file:///artists.csv' AS row
5253
MERGE (a:Artist {name: row[1], year: toInteger(row[2])})
5354
RETURN a.name, a.year
5455
----
56+
// end::clauses_load_csv_local_files[]
5557
5658
.Result
5759
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -112,12 +114,14 @@ However, this means that insecure URLs on virtual hosts will not function unless
112114
----
113115
114116
.Query
117+
// tag::clauses_load_csv_remote_locations[]
115118
[source, cypher]
116119
----
117120
LOAD CSV FROM 'https://data.neo4j.com/bands/artists.csv' AS row
118121
MERGE (a:Artist {name: row[1], year: toInteger(row[2])})
119122
RETURN a.name, a.year
120123
----
124+
// end::clauses_load_csv_remote_locations[]
121125
122126
.Result
123127
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -308,7 +312,7 @@ It also mitigates the risk of Cypher injection.
308312
(For more information about Cypher injection, see link:https://neo4j.com/developer/kb/protecting-against-cypher-injection/[Neo4j Knowledge Base -> Protecting against Cypher injection]).
309313

310314
.bands-with-headers.csv
311-
[source, csv, filename="artists-with-headers.csv"]
315+
[source, csv, filename="bands-with-headers.csv"]
312316
----
313317
Id,Label,Name
314318
1,Band,The Beatles
@@ -318,12 +322,14 @@ Id,Label,Name
318322
----
319323

320324
.Query
321-
[source, cypher, role=test-skip]
325+
// tag::clauses_load_csv_dynamic_columns[]
326+
[source, cypher]
322327
----
323328
LOAD CSV WITH HEADERS FROM 'file:///bands-with-headers.csv' AS line
324329
MERGE (n:$(line.Label) {name: line.Name})
325330
RETURN n AS bandNodes
326331
----
332+
// end::clauses_load_csv_dynamic_columns[]
327333

328334
.Result
329335
[role="queryresult",options="header,footer",cols="1*<m"]
@@ -705,6 +711,7 @@ person_tmdbId,bio,born,bornIn,died,person_imdbId,name,person_poster,person_url
705711
The below query uses a xref:subqueries/call-subquery.adoc#variable-scope-clause[variable scope clause] to import variables into the `CALL` subquery.
706712
707713
.Query
714+
// tag::clauses_load_csv_transactions[]
708715
[source, cypher]
709716
----
710717
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/persons.csv' AS row
@@ -713,6 +720,7 @@ CALL (row) {
713720
SET p.name = row.name, p.born = row.born
714721
} IN TRANSACTIONS OF 200 ROWS
715722
----
723+
// end::clauses_load_csv_transactions[]
716724
717725
.Result
718726
[source, role="queryresult"]
@@ -747,11 +755,13 @@ A common use case for this function is to generate sequential unique IDs for CSV
747755
----
748756
749757
.Query
758+
// tag::clauses_load_csv_linenumber[]
750759
[source, cypher]
751760
----
752761
LOAD CSV FROM 'file:///artists.csv' AS row
753762
RETURN linenumber() AS number, row
754763
----
764+
// end::clauses_load_csv_linenumber[]
755765
756766
.Result
757767
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -782,11 +792,13 @@ The xref:functions/load-csv.adoc#functions-file[`file()`] function provides the
782792
----
783793
784794
.Query
795+
// tag::clauses_load_csv_file[]
785796
[source, cypher, role=test-result-skip]
786797
----
787798
LOAD CSV FROM 'file:///artists.csv' AS row
788799
RETURN DISTINCT file() AS path
789800
----
801+
// end::clauses_load_csv_file[]
790802
791803
.Result
792804
[role="queryresult",options="header,footer",cols="1*<m"]
@@ -833,6 +845,7 @@ Id,Name,Year
833845
----
834846
835847
.Query
848+
// tag::clauses_load_csv_headers[]
836849
[source, cypher]
837850
----
838851
LOAD CSV WITH HEADERS FROM 'file:///artists-with-headers.csv' AS row
@@ -841,6 +854,7 @@ RETURN
841854
a.name AS name,
842855
a.year AS year
843856
----
857+
// end::clauses_load_csv_headers[]
844858
845859
.Result
846860
[role="queryresult",options="header,footer",cols="2*<m"]
@@ -876,11 +890,13 @@ If you try to import a file that doesn't use `,` as field delimiter and you also
876890
----
877891
878892
.Query
893+
// tag::clauses_load_csv_field_terminator[]
879894
[source, cypher]
880895
----
881896
LOAD CSV FROM 'file:///artists-fieldterminator.csv' AS row FIELDTERMINATOR ';'
882897
MERGE (:Artist {name: row[1], year: toInteger(row[2])})
883898
----
899+
// end::clauses_load_csv_field_terminator[]
884900
885901
.Result
886902
[source, role="queryresult"]

0 commit comments

Comments
 (0)