Maps
The following graph is used for the examples below:
N0 [
label = "{Person|name = \'Charlie Sheen\'\lrealName = \'Carlos Irwin Estévez\'\l}"
]
N0 -> N4 [
color = "#2e3436"
fontcolor = "#2e3436"
label = "ACTED_IN\n"
]
N0 -> N3 [
color = "#2e3436"
fontcolor = "#2e3436"
label = "ACTED_IN\n"
]
N0 -> N2 [
color = "#2e3436"
fontcolor = "#2e3436"
label = "ACTED_IN\n"
]
N1 [
label = "{Person|name = \'Martin Sheen\'\l}"
]
N1 -> N4 [
color = "#2e3436"
fontcolor = "#2e3436"
label = "ACTED_IN\n"
]
N1 -> N2 [
color = "#2e3436"
fontcolor = "#2e3436"
label = "ACTED_IN\n"
]
N2 [
label = "{Movie|year = 1987\ltitle = \'Wall Street\'\l}"
]
N3 [
label = "{Movie|year = 1984\ltitle = \'Red Dawn\'\l}"
]
N4 [
label = "{Movie|year = 1979\ltitle = \'Apocalypse Now\'\l}"
]
Literal maps
Cypher® supports construction of maps.
The key names in a map must be of type String.
If returned through an HTTP API call, a JSON object will be returned.
If returned in Java, an object of type java.util.Map<String,Object> will be returned.
RETURN { key: 'Value', listKey: [{ inner: 'Map1' }, { inner: 'Map2' }]}
| { key: 'Value', listKey: [{ inner: 'Map1' }, { inner: 'Map2' }]} |
|---|
|
1 row |
Map projection
Cypher supports a concept called "map projections". It allows for easily constructing map projections from nodes, relationships and other map values.
A map projection begins with the variable bound to the graph entity to be projected from, and contains a body of comma-separated map elements, enclosed by { and }.
map_variable {map_element, [, …n]}
A map element projects one or more key-value pairs to the map projection. There exist four different types of map projection elements:
-
Property selector - Projects the property name as the key, and the value from the
map_variableas the value for the projection. -
Literal entry - This is a key-value pair, with the value being arbitrary expression
key: <expression>. -
Variable selector - Projects a variable, with the variable name as the key, and the value the variable is pointing to as the value of the projection. Its syntax is just the variable.
-
All-properties selector - projects all key-value pairs from the
map_variablevalue.
The following conditions apply:
-
If the
map_variablepoints to anullvalue, the whole map projection will evaluate tonull. -
The key names in a map must be of type
String.
Examples of map projections
Find 'Charlie Sheen' and return data about him and the movies he has acted in.
This example shows an example of map projection with a literal entry, which in turn also uses map projection inside the aggregating collect().
MATCH (actor:Person { name: 'Charlie Sheen' })-[:ACTED_IN]->(movie:Movie)
RETURN actor { .name, .realName, movies: collect(movie { .title, .year })}
| actor |
|---|
|
1 row |
Find all persons that have acted in movies, and show number for each. This example introduces an variable with the count, and uses a variable selector to project the value.
MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)
WITH actor, count(movie) AS nrOfMovies
RETURN actor { .name, nrOfMovies }
| actor |
|---|
|
|
2 rows |
Again, focusing on 'Charlie Sheen', this time returning all properties from the node.
Here we use an all-properties selector to project all the node properties, and additionally, explicitly project the property age.
Since this property does not exist on the node, a null value is projected instead.
MATCH (actor:Person { name: 'Charlie Sheen' })
RETURN actor { .*, .age }
| actor |
|---|
|
1 row |