length()
returns the length of a path.
Syntax: length(path)
Returns: Integer
Arguments
Name | Description |
path | An expression that returns a path. |
Example
MATCH path=(a:Person {name: "Elon"})-->(b:Company {name:"SpaceX"})
RETURN length(path)
The value returned will be the length of the path going from the node with the name Elon to the node with the name SpaceX. In this case, the length of the path will be 1. As there is only one relationship between these 2 nodes. Also note here that, we have not specified the type of relationship between these 2 nodes for the path `path`, and hence, the path can contain all types of relationships.
Also, for all other examples here, we will be be using the same graph as shown above.
Considerations
length(null)
returnsnull
.
Example
MATCH path=(a:Person {name: "Elon"})--(b:Company {name:"SpaceX"})
RETURN length(path)
Returns the following Result
length(path) |
1 |
Note– This is similar to the above example, except that, in this the direction of the relationship won’t be checked. So, the path can go from node with name Elon to node with name SpaceX and also from the node with name SpaceX to the one with the name Elon. In this particular example however, the path goes from node Elon to node SpaceX. Hence, the output will be similar to the previous one, i.e, 1.
Example
MATCH path=(a:Person {name: "Elon"})<--(b:Company {name:"SpaceX"})
RETURN length(path)
Returns the following Result
MATCH path=(a:Person ... |
(no changes, no records) |
Note– This is similar to the above two examples, except that, in this case the direction of the relationship is reversed. So, instead of checking for the paths going from node with name Elon to the the node with the name SpaceX, it checks for paths going from node with the name SpaceX to the node with the name Elon. In this particular example however, there is no path going from node SpaceX to node Elon, and hence, no match matching records are found.
Example
MATCH path=(a:Person {name: "Elon"})<-[:CEO]-(b:Company {name:"Tesla"})
RETURN length(path)
Returns the following Result
length(path) |
1 |
Note– The output will be the length of the path going from the node with name Tesla to the node with name Elon.
Example
MATCH path=(a:Person {name: "Elon"})-->(b)-->(c)
RETURN length(path)
Returns the following Result
length(path) |
2 |
Note– There is one path going from node with name Elon to another node(say b, denoted by the placeholder) which goes to another node(say c, denoted by the placeholder). The length of this path is 2 and hence the output is 2.
Example
MATCH path=(a:Person {name: "Elon"})--(b)--(c)
RETURN length(path)
Returns the following Result
length(path) |
2 |
Note– There are two paths with a depth of 2 from the node with name Elon(say a, denoted by the placeholder). These being the path Elon–>SpaceX–>Falcon9 and Elon<–Tesla<–SolarCity. And we haven’t specified any direction in the given path. Hence, 2 is printed twice, one for each path.
Example
MATCH path=(a:Person {name: "Elon"})--(b)
RETURN length(path)
Returns the following Result
length(path) |
1 |
Note– This similar to the above example, just that there are 4 nodes that are at a depth of 1 from the node with the name Elon. Hence, 1 is printed 4 times.
Example
RETURN length(null)
Returns the following Result
length(null) |
null |
Note– length(null)
gives null
as output.