Maps
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'}]} |
---|
|
Rows: 1 |
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_variable
as 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_variable
value.
The following conditions apply:
-
If the
map_variable
points to anull
value, 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)
WITH actor, collect(movie{.title, .year}) AS movies
RETURN actor{.name, .realName, movies: movies}
actor |
---|
|
Rows: 1 |
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 nbrOfMovies
RETURN actor{.name, nbrOfMovies}
actor |
---|
|
|
Rows: 2 |
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 |
---|
|
Rows: 1 |
Was this page helpful?