Map projections
Many applications that access Neo4j via their drivers use Cypher to retrieve data from the graph as objects that will be used by the application. In Neo4j Browser, when nodes are returned, you can either view them as a graph, or you can view them in table view where all properties for a node are a single row. The data is returned as rows of data where each row represents a JSON-style object for a node.
MATCH (p:Person)
WHERE p.name CONTAINS "Thomas"
RETURN p AS person
ORDER BY p.name ASC
This query returns all Person nodes that contain the string "Thomas". If you view the data returned as a table, it returns internal node information such as labels and identity, along with the property values.
Another way that you can return data is without the internal node information, that is, only property values.
MATCH (p:Person)
WHERE p.name CONTAINS "Thomas"
RETURN p { .* } AS person
ORDER BY p.name ASC
This query returns an object named person that contains all of the property values for the node. It does not contain any of the internal information for the node such as its labels or id.
Additionally, you can customize what properties you return in the objects.
MATCH (p:Person)
WHERE p.name CONTAINS "Thomas"
RETURN p { .name, .born } AS person
ORDER BY p.name
Here the person objects returned will include the name and born properties.
Being able to customize how data is returned is useful, especially if you are using a programming language that can work with JSON-style objects.
Here is an example, where we are adding information to the objects returned that are not part of the data in the graph.
MATCH (m:Movie)<-[:DIRECTED]-(d:Director)
WHERE d.name = 'Woody Allen'
RETURN m {.*, favorite: true} AS movie
In addition to returning all property values for each Woody Allen movie, we are returning a property of favorite with a value of true for each Movie object returned.
Check your understanding
1. Returning selected properties
We want to return the title and release date as Movie objects for all Woody Allen movies. Select the correct RETURN clause to do this.
Once you have selected your option, click the Check Results query button to continue.
MATCH (m:Movie)<-[:DIRECTED]-(d:Director)
WHERE d.name = 'Woody Allen'
RETURN /*select:m {.title, .released}*/
ORDER BY m.released
-
✓
m {.title, .released} AS movie
-
❏
m {title; released} AS movie
-
❏
m {.Movie} AS titleReleased
-
❏
Movie {.title, .released}
Hint
You can use a map projection to specify a subset of properties from a node or relationship.
In this case, value within the curly braces that starts with a dot (.
) will be treated as a property of the m
node.
Solution
The answer is m {.title, .released}
.
2. Returning data
Given this code:
MATCH (p:Person)
WHERE p.name CONTAINS "Thomas"
RETURN p AS person ORDER BY p.name
What is returned in every row?
-
✓ labels
-
✓ identity
-
✓ elementId
-
❏ type
-
✓ properties
Hint
Each node has a set of labels, identity, elementId and properties.
Solution
The correct answers are labels, identity, elementId, type and properties.
Summary
In this lesson, learned how you can customize rows returned in a query.
In the next lesson, you will learn how to customize data returned.