Introduction
Let’s take a look at the types of data returned by a Cypher query.
The majority of the types returned by a Cypher query are mapped directly to Go types, but some more complex types need special handling.
-
Graph types - Nodes, Relationships and Paths
-
Temporal types - Dates and times
-
Spatial types - Points and distances
Returning graph types
When graph types are returned by a query executed in the Query window, they are visualized in a graph layout.
| Go Type | Neo4j Cypher Type |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Graph types
The following code snippet finds all movies with the specified title and returns person, acted_in and movie.
result, err := neo4j.ExecuteQuery(ctx, driver, `
MATCH path = (person:Person)-[actedIn:ACTED_IN]->(movie:Movie {title: $title})
RETURN path, person, actedIn, movie
`, map[string]any{"title": "Toy Story"}, neo4j.EagerResultTransformer)Nodes
Nodes are returned as a neo4j.Node object.
for _, record := range result.Records {
node, _ := record.Get("movie")
movieNode := node.(neo4j.Node)
}fmt.Println(movieNode.ElementId) // (1)
fmt.Println(movieNode.Labels) // (2)
fmt.Println(movieNode.Props) // (3)
if name, ok := movieNode.Props["name"]; ok { // (4)
fmt.Println("name:", name)
}-
The
ElementIdproperty provides access to the node’s element ID
eg.4:97b72e9c-ae4d-427c-96ff-8858ecf16f88:0 -
The
Labelsproperty is a slice containing an array of labels attributed to the Node
eg.["Person", "Actor"] -
The
Propsproperty provides access to the node’s properties as a map.
eg.{name: "Tom Hanks", tmdbId: "31"} -
A single property can be retrieved using the
Propsmap.
Relationships
Relationships are returned as a neo4j.Relationship object.
actedIn, _ := record.Get("actedIn")
relationship := actedIn.(neo4j.Relationship)
fmt.Println(relationship.ElementId) // (1)
fmt.Println(relationship.Type) // (2)
fmt.Println(relationship.Props) // (3)
if role, ok := relationship.Props["role"]; ok { // (4)
fmt.Println("role:", role)
}-
ElementId- Internal ID of the relationship (eg.9876) -
Type- Type of relationship (eg.ACTED_IN) -
Props- Returns relationship properties as a map (eg.{role: "Woody"}) -
Individual properties can be accessed using the
Propsmap.
Paths
A path is a sequence of nodes and relationships and is returned as a neo4j.Path object.
path, _ := record.Get("path")
pathObj := path.(neo4j.Path)
fmt.Println(len(pathObj.Nodes)) // (1)
fmt.Println(len(pathObj.Relationships)) // (2)
// Iterate over nodes in the path
for i, node := range pathObj.Nodes { // (3)
fmt.Printf("Node %d: %v\n", i, node.Labels)
}
// Iterate over relationships in the path
for i, rel := range pathObj.Relationships { // (4)
fmt.Printf("Relationship %d: %s\n", i, rel.Type)
}-
len(pathObj.Nodes)- The number of nodes in the path -
len(pathObj.Relationships)- The number of relationships within the path -
pathObj.Nodes- A slice ofneo4j.Nodeobjects that can be iterated over -
pathObj.Relationships- A slice ofneo4j.Relationshipobjects that can be iterated over
Check your understanding
Summary
In this lesson, you learned about the types of data returned by a Cypher query and how to work with them in your application.
Now it’s time to test yourself on what you’ve learned.