|
4 | 4 |
|
5 | 5 | Map expressions allow you to manipulate and query xref:values-and-types/maps.adoc[`MAP`] values in Cypher. |
6 | 6 |
|
| 7 | +[NOTE] |
| 8 | +The xref:functions/list.adoc#functions-keys[`keys()`] function can be used to return a `LIST` of all the keys in a `MAP`. |
| 9 | + |
7 | 10 | [[example-graph]] |
8 | 11 | == Example graph |
9 | 12 |
|
10 | 13 | The following graph is used for the examples below: |
11 | 14 |
|
12 | | -image::values_and_types_maps_graph.svg[width="600",role="middle"] |
| 15 | +image::values_and_types_maps_graph.svg[width="700",role="middle"] |
13 | 16 |
|
14 | 17 | To recreate the graph, run the following query against an empty Neo4j database: |
15 | 18 |
|
@@ -328,23 +331,22 @@ RETURN map{a: map.a, valueSum: map.a + map.b + map.c} AS projectedMap |
328 | 331 | 1+d|Rows: 1 |
329 | 332 | |=== |
330 | 333 |
|
331 | | -The below query finds the `Keanu Reeves` node and the movies he has acted in. |
332 | | -It is an example of a map projection with a literal entry, which in turn also uses map projection inside the aggregating xref:functions/aggregating.adoc#functions-collect[collect()] function. |
| 334 | +This query uses a map projection with a literal entry, where the `size(movies)` expression calculates the total number of movies `Keanu Reeves` has acted in. |
333 | 335 |
|
334 | 336 | .Map projection with a literal entry |
335 | 337 | [source, cypher, indent=0] |
336 | 338 | ---- |
337 | 339 | MATCH (keanu:Person {name: 'Keanu Reeves'})-[:ACTED_IN]->(movie:Movie) |
338 | | -WITH keanu, collect(movie{.title, .released}) AS movies |
339 | | -RETURN keanu{.name, movies: movies} AS keanuMovies |
| 340 | +WITH keanu, collect(movie) AS movies |
| 341 | +RETURN keanu {.name, totalMovies: size(movies)} AS keanuDetails |
340 | 342 | ---- |
341 | 343 |
|
342 | 344 | .Result |
343 | 345 | [role="queryresult",options="header,footer",cols="1*<m"] |
344 | 346 | |=== |
345 | | -| keanuMovies |
| 347 | +| keanuDetails |
346 | 348 |
|
347 | | -| {name: "Keanu Reeves", movies: [{title: "The Matrix Revolutions", released: 2003}, {title: "The Matrix Reloaded", released: 2003}, {title: "The Matrix", released: 1999}, {title: "The Devils Advocate", released: 1997}, {title: "The Matrix Resurrections", released: 2021}]} |
| 349 | +| {name: "Keanu Reeves", totalMovies: 5} |
348 | 350 |
|
349 | 351 | 1+d|Rows: 1 |
350 | 352 | |=== |
@@ -381,17 +383,17 @@ It uses the xref::functions/aggregating.adoc#functions-count[count()] function t |
381 | 383 | [source, cypher] |
382 | 384 | ---- |
383 | 385 | MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie) |
384 | | -WITH actor, count(movie) AS numberOfMovies |
385 | | -RETURN actor{numberOfMovies, .name} AS nameAndMovies |
| 386 | +WITH actor, count(movie) AS totalMovies |
| 387 | +RETURN actor{totalMovies, .name} AS nameAndMovies |
386 | 388 | ---- |
387 | 389 |
|
388 | 390 | .Result |
389 | 391 | [role="queryresult",options="header,footer",cols="1*<m"] |
390 | 392 | |=== |
391 | 393 | | nameAndMovies |
392 | 394 |
|
393 | | -| {name: "Keanu Reeves", numberOfMovies: 5} |
394 | | -| {name: "Carrie-Anne Moss", numberOfMovies: 4} |
| 395 | +| {name: "Keanu Reeves", totalMovies: 5} |
| 396 | +| {name: "Carrie-Anne Moss", totalMovies: 4} |
395 | 397 |
|
396 | 398 | 1+d|Rows: 2 |
397 | 399 | |=== |
|
0 commit comments